Created
June 18, 2014 14:59
-
-
Save akfreas/0cd345065b6d9bd780e5 to your computer and use it in GitHub Desktop.
NSURLRequest Curl Command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface NSURLRequest (CURL) | |
- (NSString *)curlCommand; | |
@end | |
@implementation NSURLRequest (CURL) | |
- (NSString *)curlCommand | |
{ | |
NSDictionary *params = [NSJSONSerialization JSONObjectWithData:self.HTTPBody options:0 error:nil]; | |
NSMutableString *paramString = [NSMutableString stringWithString:@"{"]; | |
for (NSString *key in params) { | |
NSString *param = [NSString stringWithFormat:@"\"%@\" : \"%@\", ", key, params[key]]; | |
[paramString appendString:param]; | |
} | |
[paramString appendString:@"}"]; | |
NSMutableString *headerString = [NSMutableString string]; | |
for (NSString *key in [self allHTTPHeaderFields]) { | |
[headerString appendFormat:@" -H \"%@: %@\"", key, [self valueForHTTPHeaderField:key]]; | |
} | |
NSString *command = [NSString stringWithFormat:@"curl -X %@ %@ -d '%@' %@", self.HTTPMethod, headerString, paramString, [self.URL absoluteString]]; | |
return command; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment