Skip to content

Instantly share code, notes, and snippets.

@andrei512
Created January 6, 2014 13:20
Show Gist options
  • Save andrei512/8282790 to your computer and use it in GitHub Desktop.
Save andrei512/8282790 to your computer and use it in GitHub Desktop.
NSURLRequest+cURL
#import <Foundation/Foundation.h>
@interface NSURLRequest (cURL)
- (NSString *)cURLCommand;
@end
#import "NSURLRequest+cURL.h"
@implementation NSURLRequest (cURL)
- (NSString *)cURLCommand {
NSMutableString *string = [NSMutableString string];
[string appendString:@"curl -k -i "];
if ([self.HTTPMethod isEqualToString:@"POST"]) {
NSString *params = [[NSString alloc] initWithData:self.HTTPBody encoding:NSUTF8StringEncoding];
params = [params stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
params = [params stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
[string appendFormat:@"-d \"%@\" ",params];
}
for (NSString *headerKey in [self.allHTTPHeaderFields allKeys]) {
NSString *value = [self.allHTTPHeaderFields objectForKey:headerKey];
[string appendFormat:@"-H \"%@\":\"%@\" ", headerKey, value];
}
[string appendString:[NSString stringWithFormat:@"\"%@\"",self.URL.absoluteString]];
return [NSString stringWithString:string];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment