Talha Ch Talha Ch - 1 year ago 164
Objective-C Question

AFNetworking 3 AFMultipartFormData getting bytes data in response

I'm doing this to send images in an array to server:

NSString *string = [NSString stringWithFormat:@"%@/API/Upload",BaseURLString];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithBaseURL:[NSURL URLWithString:BaseURLString]];

[manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
[manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];

for(NSData *eachImage in self.fetchedAtt) {
[manager POST:string
constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {

[formData appendPartWithFormData:eachImage


success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {

if (responseObject != nil)
//NSDictionary *jsonDictionary = responseObject;

failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {


For now I only send one image to test. And it get
which is bytes data.

<696d6167 65313a20 efbfbdef bfbdefbf bdefbfbd 00104a46 49460001
01000048 00480000 efbfbdef bfbd0058 45786966 00004d4d 002a0000
00080002 01120003 00000001 00010000 efbfbd69 00040000 00010000
00260000 00000003 efbfbd01 00030000 00010001 0000efbf bd020004
00000001 0000001e efbfbd03...

As per the web guy response should be the URL of the image. Web backend is in
What am I doing wrong?


If I change the response serializer I get:

Request failed: unacceptable content-type: application/octet-stream

I've even tried this:

NSString *string = [NSString stringWithFormat:@"%@/API/Upload",BaseURLString];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
[manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
NSError *error;

for(NSData *eachImage in self.fetchedAtt) {

NSURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:string parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//NSString *value = @"qux";
//NSData *data = [value dataUsingEncoding:NSUTF8StringEncoding];

//[formData appendPartWithFormData:eachImage name:@"image1"];
[formData appendPartWithFileData:eachImage name:@"image1" fileName:@"test.jpg" mimeType:@"image/jpeg"];
} error:&error];

NSURLSessionDataTask *task = [manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error.localizedDescription);

NSLog(@"%@", responseObject);
[task resume];

But got
Request failed: internal server error (500)
. if I uncomment the
and comment other line I get same bytes in

More Updates:

We tested it on postman and the image was uploaded successfully. But if I give something in parameter I get

Request failed: unsupported media type (415)

Rob Rob
Answer Source

When you upload images, you'd expect some response from the server (just to acknowledge that they were received successfully). Having said that, looking at your hex data, it's curious. What it looks like is:

enter image description here

The "JFIF" and "Exif" references are consistent with a JPEG response. And the image1 looks like it was the name of the image you sent. But this is a very curious response. It's definitely not the URL of the image.

You should contact the web service authors and get details of what precisely they're sending back, because it doesn't conform to standard responses (XML, JSON, etc.).

By the way, your method of uploading the image looks incorrect. You'd usually do something like:

[formData appendPartWithFileData:eachImage

Clearly, you'd specify the fileName and mimeType to match what the NSData really was. And I'd double check with the author of the web service whether they really want to use a field name of image1 ... that's a little atypical.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download