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) {
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) {
// [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)