user2381124
user2381124

Reputation: 135

Error Domain=NSURLErrorDomain Code=-1017 "cannot parse response"

Here I am getting error while I post json.

Error Domain=NSURLErrorDomain Code=-1017 "cannot parse response" UserInfo=0x7f89dac01a40 {NSUnderlyingError=0x7f89e0277a20 "cannot parse response", NSErrorFailingURLStringKey=http://test-onboard.qlc.in/FieldSense/authenticate, NSErrorFailingURLKey=http://test-onboard.qlc.in/FieldSense/authenticate, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-1, NSLocalizedDescription=cannot parse 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];

Upvotes: 11

Views: 17274

Answers (4)

DevB2F
DevB2F

Reputation: 5075

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

let headers = [
     "Cookie": "",
]

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

This gave no error:

let headers = [
    "Cookie": "",
    "Authorization" : "Token " + token
]

Alamofire.request(urlString, method: .get, encoding: JSONEncoding.default, headers: headers).responseJSON {

Upvotes: 0

Daniel Kuta
Daniel Kuta

Reputation: 1634

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 variable params was dict [:]. I changed to parameters: nil and it's work.

Upvotes: 1

priyanka gautam
priyanka gautam

Reputation: 377

I am also getting this error but i overcome this by adding a line

request.HTTPMethod = "POST"

for Objective-C programming use this:

[request setHTTPMethod:@"POST"];

Upvotes: 8

aBilal17
aBilal17

Reputation: 3132

For any other people who got error code -1017 -- I fixed it by manually setting my http headers in my http request. I think the server was having problems parsing the HTTP headers because instead of the string "Authorization" : "qii2nl32j2l3jel" my headers didn't have the quotes like this: Authorization : "1i2j12j". Good luck.

Something like this:

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];

Upvotes: 0

Related Questions