• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Translate curl command to NSURLRequest
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Translate curl command to NSURLRequest


  • Subject: Translate curl command to NSURLRequest
  • From: Antonio Nunes <email@hidden>
  • Date: Sat, 04 Jul 2015 10:52:57 +0100

I have the following curl command to a web api, which retrieves some info:
curl -X GET -H 'Authorization: Basic blabla' -H 'Content-Type: application/xml; charset=utf-8' -H 'Accept-Language: en' -d "<user><account_attributes><email>email@hidden</email><password>SomePassWord</password></account_attributes></user>" 'https://example.com/api/v1/endpoint

The -d and xml-string are mandatory for this GET command. I haven’t been able to translate this into an equivalent NSURLRequest that gets accepted by the server.

I tried this:
   NSURLCredential *credential = [[WRTSServerEngine sharedServerEngine] savedCredentialsForHost:@“example.com"
                                                                                           port:0
                                                                                       protocol:@"https"
                                                                                          realm:@“SomeRealm"];

   NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://example.com/api/v1/endpoint";]];

   NSString *authStr = [NSString stringWithFormat:@"%@:%@", credential.user, credential.password];
   NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
   NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];
   [request setValue:authValue forHTTPHeaderField:@"Authorization"];

   NSString *infoAsXMLString = @"<user>";
   infoAsXMLString = [infoAsXMLString stringByAppendingString:@"<account_attributes>"];
   infoAsXMLString = [infoAsXMLString stringByAppendingFormat:@"<email>%@</email>", credential.user];
   infoAsXMLString = [infoAsXMLString stringByAppendingFormat:@"<password>%@</password>", credential.password];
   infoAsXMLString = [infoAsXMLString stringByAppendingString:@"</account_attributes>"];
   infoAsXMLString = [infoAsXMLString stringByAppendingString:@"</user>"];
   [request setHTTPBody:[infoAsXMLString dataUsingEncoding:NSUTF8StringEncoding]];

   NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
   sessionConfig.allowsCellularAccess = YES;
   [sessionConfig setHTTPAdditionalHeaders:@{@"Accept": @"application/xml"}];
   NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig];
   self.task = [session dataTaskWithRequest:request
                          completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                              NSLog(@"%@", error);
                          }];
   [self.task resume];

This results in the following error:
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo=0x7ffd0b5f7a00 {NSUnderlyingError=0x7ffd0b58be50 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1005.)", NSErrorFailingURLStringKey=https://staging.wrts.nl/api/v1/existing_user, NSErrorFailingURLKey=https://staging.wrts.nl/api/v1/existing_user, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSLocalizedDescription=The network connection was lost.}

If I do not set an HHTPBody on the request, I get a 500 error (which makes sense, since the server is expecting a payload in the body).

Is there a way to see exactly what the request looks like when it goes out? Is there a way to create the request such that it is equivalent to the curl command at the top of this post?

Thanks,
António
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Translate curl command to NSURLRequest
      • From: Rick Mann <email@hidden>
    • Re: Translate curl command to NSURLRequest
      • From: Jerry Krinock <email@hidden>
  • Prev by Date: Re: Problem having a framework working in one of my targets (a screensaver)
  • Next by Date: Re: Translate curl command to NSURLRequest
  • Previous by thread: Re: What is "TypeA -> TypeB"?
  • Next by thread: Re: Translate curl command to NSURLRequest
  • Index(es):
    • Date
    • Thread