AFNetworking 3 AFMultipartFormData receiving bytes of data in response

I do this to send images to an array on the 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 parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { [formData appendPartWithFormData:eachImage name:@"image1"]; } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { if (responseObject != nil) { //NSDictionary *jsonDictionary = responseObject; NSLog(@"%@",responseObject); } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"%@",error.localizedDescription); }]; } 

So far I am sending only one image for testing. And it gets responseObject , which is a data byte.

<696d6167 65313a20 efbfbdef bfbdefbf bdefbfbd 00104a46 49460001 01000048 00480000 efbfbdef bfbd0058 45786966 00004d4d 002a0000 00080002 01120003 00000001 00010000 efbfbd69 00040000 00b0000 00010000 00b0000 00010000 00b0000 00010000 00010000 0001 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 001 000 001 000 001 000 001 000 001 000 001 000 001 000 001 000 001 000 001 000 001 000 001 000 001 000 000.

According to the response of the web guy there should be an image url. The web server is in ASP.NET What am I doing wrong?

UPDATE
If I change the response serializer, I get:

Request Error: Inappropriate Content Type: application / octet-stream

I 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); return; } NSLog(@"%@", responseObject); }]; [task resume]; } 

But received Request failed: internal server error (500) . if I uncomment appendPartWithFormData and comment out another line, I get the same bytes in responseObject .

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

Request failed: unsupported media type (415)

+1
ios objective-c multipartform-data afnetworking
source share
1 answer

When you upload images, you expect a response from the server (just to confirm that they were received successfully). Having said that, looking at your hexadecimal data, this is curious. What does it look like:

enter image description here

The "JFIF" and "Exif" links are consistent with the JPEG response. And image1 looks like it was the name of the image you submitted. But this is a very interesting answer. This is definitely not the image url.

You should contact the authors of the web services and get detailed information about what they send back, because it does not correspond to standard answers (XML, JSON, etc.).


By the way, your way of loading the image does not look right. You usually do something like:

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

Obviously, you should specify fileName and mimeType according to what NSData actually. And I would double check with the author of the web service whether they really want to use the field name image1 ... which is a bit atypical.

+1
source share

All Articles