所以我正在测试AFNetworking的多部分表单提交。我设置了参数,为了测试NSData,我创建了一个UIImage并将该图像转换为NSData。您可以想象,我希望用户填写并上传图片,并让AFNetworking负责发送我的帖子请求。
不知道这不起作用?我收到了400 Bad Request错误。
注意:假设我的“路径”参数正确,“params”都设置正确。
UIImage *myImage = [UIImage imageNamed:@"pekkle"];
NSData *imageData = UIImagePNGRepresentation(myImage);
// Setup path and parameters
NSDictionary *params = @{
@"description": description,
@"title": title,
@"userId": userId,
@"collectionId": collectionId
};
// Setup manager and perform action
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:path parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFormData: imageData name:@"title"];
} success:^(AFHTTPRequestOperation *operation, id responseObject){
NSLog(@"success");
} failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"failure %@", error);
}];
这是错误
AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8a9feb0> { URL: http://myurl.com/file? } { status code: 400, headers {
"Content-Encoding" = gzip;
"Content-Length" = 624;
"Content-Type" = "application/json; charset=utf-8";
Vary = "Accept-Encoding";
} }, NSLocalizedDescription=Request failed: bad request (400)}