Created
March 11, 2016 20:21
-
-
Save stephanecopin/9ee721a0bceba0028cba to your computer and use it in GitHub Desktop.
NSURLRequest+CURLRequest
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 (CURLRequest) | |
@property (nonatomic, copy, readonly) NSString * CURLRequest; | |
@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+CURLRequest.h" | |
@implementation NSURLRequest (CURLRequest) | |
- (NSString *)CURLRequest { | |
__block NSMutableString * displayString = [NSMutableString stringWithFormat:@"curl -L -X %@", self.HTTPMethod]; | |
[self.allHTTPHeaderFields enumerateKeysAndObjectsUsingBlock:^(id key, id val, BOOL *stop) { | |
[displayString appendFormat:@" -H \"%@: %@\"", key, val]; | |
}]; | |
[displayString appendFormat:@" \"%@\"", self.URL.absoluteString]; | |
if(![self.HTTPMethod isEqualToString:@"GET"]) { | |
NSString * bodyString = [[NSString alloc] initWithData:self.HTTPBody | |
encoding:NSUTF8StringEncoding]; | |
[displayString appendFormat:@" -d \"%@\"", [bodyString stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]]; | |
} | |
return displayString; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment