Created
January 6, 2014 13:20
-
-
Save andrei512/8282790 to your computer and use it in GitHub Desktop.
NSURLRequest+cURL
This file contains 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
#import <Foundation/Foundation.h> | |
@interface NSURLRequest (cURL) | |
- (NSString *)cURLCommand; | |
@end |
This file contains 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
#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