AFNetworking says "cannot parse response"

13,883

Solution 1

If iOS cannot parse response, the problem must be in improper format http response from server, even if You are said, that everything is 100% correct, bacause other services communicate with it.

In my case an Android app communicated successfully, because network framework used there was not so restrictive and even if the content-length of http response does not fit the actual length of data, it could read this message, unlike ios.

Use tcpdump to check http communication!

Solution 2

I got the same problem, and the reason is that the API require GET http method, and I need pass a parameter, I send it by the following code:

[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

When I execute the request with GET method, it will report the error.

My solution is when the API need GET method, the parameter should add on the URL, such as http://apiserver/api?paramName=paramValue, when the API method is POST, then use the code above, set the HTTPBody for the request object.

Solution 3

I got the same issue when I tried to call a web method using http GET method instead of http POST. The web service method was expecting POST request not GET. Hope this information will help some one.

Solution 4

If you are using (iOS8 has a problem with this but works in iOS7)

NSData *dataToSend = [jsonData dataUsingEncoding:NSUTF16BigEndianStringEncoding] 

to send to the server, please make it as per below:

NSData *dataToSend = [jsonData dataUsingEncoding:NSUTF8StringEncoding]

Solution 5

I was seeing this error because a framework I was using was trying to send URL parameters in a GET request inside a JSON encoded body (instead of as URL-encoded parameters in the URL string as it should have been). That was causing the server send back an "invalid parameters message" which the client couldn't handle and instead aborted mid-request. Switching from body parameters to URL-encoded parameters for GET requests fixed the problem.

Share:
13,883
galvanize
Author by

galvanize

Updated on June 07, 2022

Comments

  • galvanize
    galvanize about 2 years

    I am sending a request to server, which is handling the request and responsding. However on my app I am receiving:

    Error Domain=NSURLErrorDomain Code=-1017 "cannot parse response" UserInfo=0x167668d0     
    {NSErrorFailingURLStringKey=https://***, _kCFStreamErrorCodeKey=-1, 
    NSErrorFailingURLKey=https://***/, NSLocalizedDescription=cannot
    parse response, _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x16731990 "cannot parse
    response"
    

    Accept field in http request is adequate.

    I cannot even see what messege is arriving, because NSHTTPURLResponse object is null.

    What can be an issue and in what way I can see what message is coming not using things like wireshark.

    Thank You!