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 protected]</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 protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/macnetworkprog/archive%40mail-archive.com
This email sent to [email protected]