【发布时间】:2014-12-24 10:53:26
【问题描述】:
我正在使用 AFNetworking 2.0,在此我必须在正文部分传递 json 对象,如何在 AFHTTPRequestOperationManager 中传递 HTTP 正文。
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:@"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
谢谢,
【问题讨论】:
-
我建议
appendWithFormData:name:。从理论上讲,如果您也觉得有必要指定Content-Type,您可以使用appendPartWithHeaders:body:,手动指定Content-Disposition和Content-Type标头,但这似乎更麻烦并且可能没有必要。 -
我需要在上面的方法中添加这个方法吗?
-
在
constructingBodyWithBlock块内。在您的appendPartWithFileURL之前或之后。 -
我试过这样[formData appendWithFormData - 但是没有appendWithFormData方法,我需要通过HTTPBody
-
我想要这个 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [请求 setHTTPBody:postBody];
标签: json xcode ios7 afnetworking-2