Category for NSMutableURLRequest to log curl command line prompts for debugging purposes
Drag the directory MKCurlAdditions into your project. There should be two files in it.
- NSMutableURLRequest+MKCurlAdditions.h
- NSMutableURLRequest+MKCurlAdditions.m
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. This code comes straight from MKNetworkKit and you don't need this category if you are using MKNetworkKit as your networking library.
If you are using AFNetworking, I would recommend overriding the method,
- (void)enqueueHTTPRequestOperationWithRequest:(NSURLRequest *)request
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.
- (void)enqueueHTTPRequestOperationWithRequest:(NSURLRequest *)request
success:(void (^)(id object))success
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))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!
This code is licensed under MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.