Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking log request body of multipart/form-data

Say I have a multipart/form-data constructed as below

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);
    }];

Here is what AFNetworkActivityLogger logs. The body is (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)

How do I log the request body what sent to the API?

like image 871
imObjCSwifting Avatar asked Nov 10 '22 15:11

imObjCSwifting


1 Answers

AFNetworkActivityLogger does not output the body of a multipart form request because the request has not HTTPBody property, instead it has an HTTPBodyStream. See here:

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

In order to examine the body of the request, follow these steps:

Subscribe to the notification: com.alamofire.networking.task.resume Get the request from the notification: NSURLRequest *request = AFNetworkRequestFromNotification(notification); Read the stream from the request: [request HTTPBodyStream]

like image 180
Vincil Bishop Avatar answered Nov 14 '22 22:11

Vincil Bishop