Http Status 415 When Using Alamofire Multipart Upload

AFNetworking 2 - Image upload - Request failed: unsupported media type (415)

With the support of Mattijs from TinyPNG I've got it working! Thanks Mattijs!

The problem was, that the TinyPNG API expects the request body to only be the image data, which isn't the case with the multipart form data body I used in my original code.

My working solution is:

-(void) uploadImage:(NSImage *)image {

NSData *imageData = [self PNGRepresentationOfImage:image];

NSURL *url = [NSURL URLWithString:TINY_PNG_URL];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:imageData];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"image/png" forHTTPHeaderField:@"Content-Type"];

NSString *authStr = [NSString stringWithFormat:@"%@:%@", USERNAME, PASSWORD];
NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id result) {
NSLog(@"Success: %@ ***** %@", operation.responseString, result);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];
[operation start];
}

Http Status 415 even when providing correct ContentType

Got it - was my own faulty logic that screwed me up. The first request header wasn't formatted properly and I needed to send my information as a value:

var XmlHttpRequest = require("xmlhttprequest").XMLHttpRequest;
console.log("references loaded");

var xhr = new XmlHttpRequest();
xhr.open("POST", "https://developer.api.autodesk.com/authentication/v1/authenticate", false);

//var headerRequest = "Content-Type:application/x-www-form-urlencoded";
var headerValue =
"client_id=_someSecretValue_" + "&" +
"client_secret=_otherSecretValue_" + "&" +
"grant_type=client_credentials" + "&" +
"scope=data:read";

xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(headerValue);

console.log(xhr.status);
console.log(xhr.statusText);

AFMultipartFormData - Unsupported media type

I found out the solution. As Jeffery Thomas wrote, you can't change the content type of the request, and there was the problem. Specifically, there was no problem at all sending the image but the json object which needed the name "registration" AND the content-type and i couldn't find the proper way to pass both. So, I made up the headers needed to do that (pass the content-type and the name).

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"http://vext.dsigned.gr:8080/auth/account/email" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

NSMutableDictionary *headers = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"application/json; charset=UTF-8",@"Content-Type", @"form-data; name=\"registration\"",@"Content-Disposition", nil];
[formData appendPartWithHeaders:headers body:jsonData];
[formData appendPartWithFileData:imageData name:@"pic" fileName:@"avatar.png" mimeType:@"image/png"];

} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"operation=%@",operation);
NSLog(@"Response: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"operation=%@",operation);
NSLog(@"Error: %@", error);
}];

windows server returns 404 when tried to request POST using Alamofire (swift)

You can use Alamofire.upload and this is going to work

    Alamofire.upload(multipartFormData: { (MultipartFormData) in
for (key, value) in params {
if let data = value.data(using: .utf8) {
MultipartFormData.append(data, withName: key)
}
}
}, usingThreshold: UInt64.init(), to: url, method: .post, headers: header) { (results) in

}

iOS: Getting 415(unsupported media type) error while consuming RESTFUL web service using NSUrlConnection

You receive HTTP status code 415 when the Content-Type you sent with the request is not a support type. Looking at the code, you are incorrectly setting the Content-Type header. This line:

[request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"contentType"];

should be

[request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];


Related Topics



Leave a reply



Submit