Re: Pass an htttpbody on a GET request with NSURLRequest
Re: Pass an htttpbody on a GET request with NSURLRequest
- Subject: Re: Pass an htttpbody on a GET request with NSURLRequest
- From: Jens Alfke <email@hidden>
- Date: Sun, 05 Jul 2015 07:38:50 -0700
Two discrepancies: The request sent by NSURLSession is missing the Content-Type header, and has an Accept-Encoding header (which shouldn't be necessary because NSURLSession handles compressed responses for you.)
Sending a request body with a GET is unusual, but I have no reason to think NSURLSession won't do it if you tell it to.
If you're still having trouble, you should use a tool like HTTPScoop, Charles Proxy, or tcpdump to look at what actual bits get sent over the socket.
—Jens
> On Jul 4, 2015, at 10:55 PM, 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. It appears that an HTTPBody on a GET request is not honoured by NSURLRequest.
>
> This is the code I use to make the request (on iOS):
> 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"];
> [request setValue:@"nl, en-us" forHTTPHeaderField:@"Accept-Language"];
> [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
>
> 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;
> 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 any way to get the HHTPBody sent along with the GET request?
>
> -António
>
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Macnetworkprog mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden