AFNetworking日志请求主体的multipart / form-data

时间:2015-04-21 15:19:41

标签: ios objective-c afnetworking afnetworking-2 multipartform-data

假设我有一个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);
    }];

这是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)

如何记录发送给API的请求正文?

1 个答案:

答案 0 :(得分:1)

AFNetworkActivityLogger不输出多部分表单请求的主体,因为请求没有HTTPBody属性,而是具有HTTPBodyStream。见这里:

https://github.com/AFNetworking/AFNetworkActivityLogger/blob/master/AFNetworkActivityLogger/AFNetworkActivityLogger.m#L120

要检查请求的正文,请按以下步骤操作:

订阅通知:com.alamofire.networking.task.resume 从通知中获取请求:NSURLRequest * request = AFNetworkRequestFromNotification(notification); 从请求中读取流:[请求HTTPBodyStream]