This post is a sequel to one of my previous post, Easy Debugging with ASIHTTPRequest.
I previously showed you a code snippet for logging ASIHTTPRequest as curl commands on command line.
I’ve updated that code for NSMutableURLRequest, which means, you can use it for almost any third party networking kits, including the recently famous AFNetworking.

The Drive

The main necessity for this was, I always used to miss some of the parameters I should use for calling a said API. The only way to debug these issues is to simulate the call on a third party REST client, check if everything works and then start coding. But that was a bit too overkill for me. I wrote this snippet so that, you can straight away NSLog a NSMutableURLRequest and see the curl syntax on the console. You can copy and paste this to your Terminal and check if you are calling the API correctly.

Usage

Drag the attached files into your project.

<code>NSMutableURLRequest+CurlDescription.h
NSMutableURLRequest+CurlDescription.m</code>

Add a line to your PCH file or any global header file to include this file.

That’s it!. You are done.
Now, whenever you need to print your request as a curl command, just add a NSLog it.

If you are using AFNetworking, I would recommend overriding the method,

- (void)enqueueHTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest 
                                       success:(void (^)(id object))success 
                                       failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure;

in your own APIClient class.
You can now NSLog the request and call the super class implementation.

Example

-(void) enqueueHTTPRequestOperationWithRequest:(NSURLRequest *)request success:(void (^)(id))success failure:(void (^)(NSHTTPURLResponse *, NSError *))failure {
 
    NSLog(request);
    [super enqueueHTTPRequestOperationWithRequest:request success:success failure:failure];
}

This would log every single API call you make in your app on the console as a curl command.
A second advantage of this is, it reduces cognitive overload. When you see the request printed on console, you don’t have to think what it is doing. Just copy and paste it to Terminal and start debugging right away!

Source Code

NSMutableURLRequest+Curl.zip

Licensing

Do whatever you want with the code without removing the source code attribution.


Mugunth

Follow me on Twitter

  • Jens

    Do you mind putting it on github, tag it and add a proper LICENSE file so I can add it to CocoaPods so everyone can just refer to it in their podfiles? Just in case you want to do it by your own. If you want to, I’m happy to create a repo by my self and attribute you there.