Search code examples
afnetworking-2

AFNetworking 2: What's it actually sending?


I'm using POST:parameters:constructingBodyWithBlock:. I believe the pieces I'm attempting to send to be correct, but I'm getting a HTTP 400 from the server. So something is wrong with the overall packet or how a piece of it is being encoded. I have a few theories about that, but before I do anything else I'd like to see what the data I'm sending actually looks like.

What is the best way to see what AFNetworking is actually sending for debugging purposes?

I've tried tracing quite deep, but when I reach AFMultipartBodyStream I get lost. I'm not sure how to capture its data without severely hacking at AFNetworking.


Solution

  • For anyone curious later, I've found this works nicely in the failure or success block:

    NSMutableData *data = [NSMutableData data];
    NSInputStream *stream = [operation.request.HTTPBodyStream copy];
    [stream open];
    BOOL done = NO;
    while (!done) {
        NSMutableData *buffer = [NSMutableData dataWithLength:1024];
        buffer.length = [stream read:buffer.mutableBytes maxLength:buffer.length];
        if (buffer.length) {
            [data appendData:buffer];
        } else {
            done = YES;
        }
    }
    [stream close];
    

    The resulting data object can be written to disk, but can only be converted to a string directly if the parameters were entirely string (and not a binary file).