FLEXNetworkCurlLogger.m 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //
  2. // FLEXCurlLogger.m
  3. //
  4. //
  5. // Created by Ji Pei on 07/27/16
  6. //
  7. #import "FLEXNetworkCurlLogger.h"
  8. @implementation FLEXNetworkCurlLogger
  9. + (NSString *)curlCommandString:(NSURLRequest *)request {
  10. __block NSMutableString *curlCommandString = [NSMutableString stringWithFormat:@"curl -v -X %@ ", request.HTTPMethod];
  11. [curlCommandString appendFormat:@"\'%@\' ", request.URL.absoluteString];
  12. [request.allHTTPHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *val, BOOL *stop) {
  13. [curlCommandString appendFormat:@"-H \'%@: %@\' ", key, val];
  14. }];
  15. NSArray<NSHTTPCookie *> *cookies = [NSHTTPCookieStorage.sharedHTTPCookieStorage cookiesForURL:request.URL];
  16. if (cookies) {
  17. [curlCommandString appendFormat:@"-H \'Cookie:"];
  18. for (NSHTTPCookie *cookie in cookies) {
  19. [curlCommandString appendFormat:@" %@=%@;", cookie.name, cookie.value];
  20. }
  21. [curlCommandString appendFormat:@"\' "];
  22. }
  23. if (request.HTTPBody) {
  24. NSString *body = [[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding];
  25. [curlCommandString appendFormat:@"-d \'%@\'", body];
  26. }
  27. return curlCommandString;
  28. }
  29. @end