Attach image at URL to AFNetworking 2.0 request?
Asked Answered
S

1

0

I am trying to perform this CURL request in iOS using AFNetworking 2.0:

curl -F "[email protected]" "https://image.groupme.com/pictures?access_token=token123"

using this image: http://s3.amazonaws.com/joinlook1/ffb9900d0ea123972d2fc5319ee2f9ec2abe691e.jpg

Looking at iOS Image upload via AFNetworking 2.0

I tried:

[self POST:[NSString stringWithFormat:@"pictures?access_token=%@", access_token] parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    NSError *error;
    [formData appendPartWithFileURL:[NSURL URLWithString:@"http://s3.amazonaws.com/joinlook1/ffb9900d0ea123972d2fc5319ee2f9ec2abe691e.jpg"] name:@"image" error:&error];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];

but I kept getting the error: {"errors":["http: no such file"]}

Looking at Uploading image with AFNetworking 2.0, I tried using the extended function, but kept getting the same error.

[formData appendPartWithFileURL:[NSURL URLWithString:photo.urlString] name:@"image" fileName:@"photo.jpg" mimeType:@"image/jpeg" error:&error];

I also tried downloading the file first and then uploading it with the exact code used in both of those other posts, but I kept getting the same error:

__block NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://s3.amazonaws.com/joinlook1/ffb9900d0ea123972d2fc5319ee2f9ec2abe691e.jpg"]];

NSDictionary *parameters = @{@"image":@"YES"};
[self POST:[NSString stringWithFormat:@"pictures?access_token=%@", access_token] parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFormData:imageData name:@"image"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];

Here is the full error message:

***** Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x14fa92dc0> { URL: https://image.groupme.com/pictures?access_token=XXXX } { status code: 400, headers {
"Access-Control-Allow-Credentials" = false;
"Access-Control-Allow-Headers" = "Accept, Content-Type, X-Requested-With, X-Access-Token, User-Agent, Pragma, Referrer, Cache-Control, Origin";
"Access-Control-Allow-Methods" = "POST, GET, PUT, DELETE, OPTIONS";
"Access-Control-Allow-Origin" = "*";
"Access-Control-Max-Age" = 86400;
"Cache-Control" = "must-revalidate, private, max-age=0";
Connection = "keep-alive";
"Content-Length" = 34;
"Content-Type" = "application/json;charset=utf-8";
Date = "Tue, 19 Apr 2016 09:50:42 GMT";
Server = "nginx/1.8.1";
"X-Gm-Service" = "image-service";
} }, NSErrorFailingURLKey=https://image.groupme.com/pictures?access_token=XXXXX, com.alamofire.serialization.response.error.data=<7b226572 726f7273 223a5b22 68747470 3a206e6f 20737563 68206669 6c65225d 7d0a>, NSLocalizedDescription=Request failed: bad request (400)}

It is the same error if I download the image then attach it, or if I just attach it with the URL.

How can I attach an image to a form request in AFNetworking?

Seaward answered 19/4, 2016 at 10:18 Comment(1)
Use [formData appendPartWithFileData:name:fileName:mimeType:], not [formData appendPartWithFormData:name:]Kaleb
S
0

Looks like the file part of the cURL correlated with the name parameter in the appendPartWithFileData function. If I change it to name it works:

[formData appendPartWithFileData:imageData name:@"file" fileName:@"image.jpg" mimeType:@"image/jpeg"];

But it seems I have to use that method. If I try just add the URL, it continues to fail:

[formData appendPartWithFileURL:[NSURL URLWithString:@"..."] name:@"file" fileName:@"image.jpg" mimeType:@"image/jpeg" error:&error]

even if I use the real filename from the file URL

Seaward answered 19/4, 2016 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.