将URL上的图像附加到AFNetworking 2.0请求?

时间:2016-04-19 10:18:55

标签: ios curl ios9 afnetworking-2 groupme

我正在尝试使用AFNetworking 2.0在iOS中执行此CURL请求:

curl -F "file=@picture.jpg" "https://image.groupme.com/pictures?access_token=token123"

使用此图片:http://s3.amazonaws.com/joinlook1/ffb9900d0ea123972d2fc5319ee2f9ec2abe691e.jpg

查看iOS Image upload via AFNetworking 2.0

我试过了:

[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);
}];

但我一直收到错误:{"errors":["http: no such file"]}

查看Uploading image with AFNetworking 2.0,我尝试使用扩展函数,但仍然遇到同样的错误。

[formData appendPartWithFileURL:[NSURL URLWithString:photo.urlString] name:@"image" fileName:@"photo.jpg" mimeType:@"image/jpeg" error:&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);
}];

以下是完整的错误消息:

***** 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)}

如果我下载图片然后附加图片,或者我只是将其附加到网址,则会出现同样的错误。

如何在AFNetworking中将图像附加到表单请求?

1 个答案:

答案 0 :(得分:0)

看起来cURL的file部分与name函数中的appendPartWithFileData参数相关联。如果我将其更改为name则可以:

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

但似乎我必须使用那种方法。如果我尝试添加URL,则会继续失败:

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

即使我使用文件网址中的真实文件名