Search code examples
iosobjective-ccocoa-touchafnetworking-2nsurlsession

Response Headers and URL in AFNetworking


I am using AFNetworking to upload video and Image to a server.

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:urlString parameters:dictParams constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imgData name:@"photo" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:[NSData dataWithContentsOfFile:pathVideo] name:@"video" fileName:@"video.mp4" mimeType:@"video/mp4"];
    } error:nil];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
NSProgress *progress = nil;

NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"%@ %@", response, responseObject);
    }
}];

[uploadTask resume];

But in the response I am getting Response Headers and URL along with the desired response.

{ URL: the_url.com } { status code: 200, headers {
    Connection = "Keep-Alive";
    "Content-Length" = 49;
    "Content-Type" = "text/html; charset=UTF-8";
    Date = "Wed, 06 May 2015 11:12:50 GMT";
    "Keep-Alive" = "timeout=5, max=100";
    Server = "Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips PHP/5.6.7";
    "X-Powered-By" = "PHP/5.6.7";
} } {
    error = 0;
    message = "Test Button";
    response =     (
    );
}

I have searched a lot for this but can't find anything helpful. Please help me out with this.


Solution

  • You are logging the header with the real response :

    NSLog(@"%@ %@", response, responseObject);

    You desired response is just the responseObject apparently a dictionary.

    NSLog(@"%@", responseObject);

    For example, you can access the "message" doing :

    NSLog(@"%@", responseObject[@"message"]);

    Should log : "Test Button"