AFNetworking says: "Unable to parse the answer"

I am sending a request to a server that processes the request and responds. However, in my application, I get:

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" 

The Accept field in the HTTP request is adequate.

I don’t even see what messege is doing, because the NSHTTPURLResponse object is null.

What could be the problem and how I can see what message comes in without using things like wireshark.

Thanks!

+6
source share
6 answers

If iOS cannot parse the answer, the problem should be in the wrong answer from server to server, even if you said that everything is 100% correct, while other services communicate with it.

In my case, the Android application was successfully transmitted because the network infrastructure used there was not so restrictive, and even if the length of the content of the HTTP response did not match the actual length of the data, it could read this message, unlike ios.

Use tcpdump to test http communication!

+4
source

I have the same problem, and the reason is that the API requires the GET http method, and I need to pass the parameter, I send it with the following code:

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

When I execute a request using the GET method, it will report an error.

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

+8
source

I had the same problem when I tried to call a web method using the http GET method instead of http POST. The web service method was expecting a non-GET POST request. Hope this information helps someone.

+4
source

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

 NSData *dataToSend = [jsonData dataUsingEncoding:NSUTF16BigEndianStringEncoding] 

To send to the server, please do this as shown below:

 NSData *dataToSend = [jsonData dataUsingEncoding:NSUTF8StringEncoding] 
+2
source

I saw this error because the environment used was trying to send URL parameters to the GET request inside the JSON encoded body (instead of the URL encoded parameters in the URL string, as it should be). This led to the server sending a "message about invalid parameters" that the client could not process, but instead interrupted the average request. Fixed the problem of switching body parameters to URL-encoded parameters for GET requests.

+1
source

First, I guarantee that you are sending the correct parameters both in the body and in the headers. I got this error and after I moved one of the parameters from the body to the header, I got the correct answer.

If there is any documentation for api, make sure you follow it correctly.

0
source

Source: https://habr.com/ru/post/977359/


All Articles