Re: Translate curl command to NSURLRequest
Re: Translate curl command to NSURLRequest
- Subject: Re: Translate curl command to NSURLRequest
- From: Antonio Nunes <email@hidden>
- Date: Sun, 05 Jul 2015 05:38:44 +0100
Ugh, I’ve corrected the headers. But it doesn’t make a difference regarding the result.
-António
> On 04 Jul 2015, at 19:04, Rick Mann <email@hidden> wrote:
>
> FWIW, you're setting the Accept header, not Content-Type (nor Accept-Language).
>
>> On Jul 4, 2015, at 02:52 , Antonio Nunes <email@hidden> wrote:
>>
>> 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
>
>
> --
> Rick Mann
> email@hidden
_______________________________________________
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