Error domain = NSURLErrorDomain Code = -1017 "cannot parse the answer"

Here I get an error when I send json.

Domain error = NSURLErrorDomain Code = -1017 "cannot parse the response" UserInfo = 0x7f89dac01a40 {NSUnderlyingError = 0x7f89e0277a20 "cannot parse the response", NSErrorFailingURLStringKey = http://test-onboard.qlc.in/FieldSenserautate //test-onboard.qlc.in/FieldSense/authenticate , _kCFStreamErrorDomainKey = 4, _kCFStreamErrorCodeKey = -1, NSLocalizedDescription = cannot parse the response}

Here is my code:

NSDictionary *dictionaryData=[[NSDictionary alloc]initWithObjectsAndKeys:self.txtUsername.text, @"userEmailAddress", self.txtPassword.text, @"password",nil]; NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dictionaryData options:kNilOptions error:&error]; [request setURL:url]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:jsonData]; 
+7
ios objective-c ios8
source share
4 answers

I also get this error, but I overcome it by adding a line

 request.HTTPMethod = "POST" 

for Objective-C use this:

 [request setHTTPMethod:@"POST"]; 
+7
source share

I had the same problem with Alamofire and the error caused this:

let encoding = Alamofire.ParameterEncoding.JSON let (encodedRequest, _) = encoding.encode(URLRequest, parameters: params)

My params variables were dict [:]. I changed the parameters: nil and it works.

+1
source share

For other people who received error code -1017 - I fixed it by manually setting the http headers in my http request. I think that the server had problems parsing HTTP headers, because instead of the line "Authorization": "qii2nl32j2l3jel" in my headers there were no such quotes: Authorization: "1i2j12j". Good luck.

Something like that:

 NSDictionary* newRequestHTTPHeader = [[NSMutableDictionary alloc] init]; [newRequestHTTPHeader setValue:authValue forKey:@"\"Authorization\""]; [newRequestHTTPHeader setValue:contentLengthVal forKey:@"\"Content-Length\""]; [newRequestHTTPHeader setValue:contentMD5Val forKey:@"\"Content-MD5\""]; [newRequestHTTPHeader setValue:contentTypeVal forKey:@"\"Content-Type\""]; [newRequestHTTPHeader setValue:dateVal forKey:@"\"Date\""]; [newRequestHTTPHeader setValue:hostVal forKey:@"\"Host\""]; [newRequestHTTPHeader setValue:publicValue forKey:@"\"public-read-write\""]; //the proper request is built with the new http headers. NSMutableURLRequest* request2 = [[NSMutableURLRequest alloc] initWithURL:request.URL]; [request2 setAllHTTPHeaderFields:newRequestHTTPHeader]; [request2 setHTTPMethod:request.HTTPMethod]; 
0
source share

I got this error 1017 when setting parameters in a get request:

 let headers = [ "Cookie": "", ] Alamofire.request(urlString, parameters: ["token": token],encoding: JSONEncoding.default, headers: headers).responseJSON { ... 

This did not give an error:

 let headers = [ "Cookie": "", "Authorization" : "Token " + token ] Alamofire.request(urlString, method: .get, encoding: JSONEncoding.default, headers: headers).responseJSON { 
0
source share

All Articles