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>y...@example.com</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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to