Skip to content

Instantly share code, notes, and snippets.

@stephanecopin
Created March 11, 2016 20:21
Show Gist options
  • Save stephanecopin/9ee721a0bceba0028cba to your computer and use it in GitHub Desktop.
Save stephanecopin/9ee721a0bceba0028cba to your computer and use it in GitHub Desktop.
NSURLRequest+CURLRequest
#import <Foundation/Foundation.h>
@interface NSURLRequest (CURLRequest)
@property (nonatomic, copy, readonly) NSString * CURLRequest;
@end
#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