imO*_*ing 5 multipartform-data objective-c ios afnetworking afnetworking-2
假设我有一个multipart/form-data构造如下
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);
}];
Run Code Online (Sandbox Code Playgroud)
这是AFNetworkActivityLogger记录的内容.正文是(null).
POST 'http://example.com/resources.json': {
"Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5";
"Content-Length" = 206123;
"Content-Type" = "multipart/form-data; boundary=Boundary+A13DFC7B7D81B98A";
"User-Agent" = "Example/0.2.3 (iPad Simulator; iOS 8.3; Scale/2.00)";
} (null)
Run Code Online (Sandbox Code Playgroud)
如何记录发送给API的请求正文?
AFNetworkActivityLogger 不会输出多部分表单请求的正文,因为该请求没有 HTTPBody 属性,而是具有 HTTPBodyStream。看这里:
要检查请求的正文,请按照下列步骤操作:
订阅通知:com.alamofire.networking.task.resume 从通知中获取请求: NSURLRequest *request = AFNetworkRequestFromNotification(notification); 从请求中读取流:[request HTTPBodyStream]
| 归档时间: |
|
| 查看次数: |
1507 次 |
| 最近记录: |