我正在尝试使用 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
查看通过 AFNetworking 2.0 上传的 iOS 图像
我试过了:
[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)}
如果我下载图像然后附加它,或者我只是将它与 URL 一起附加,则会出现同样的错误。
如何在 AFNetworking 中将图像附加到表单请求?