How to get NSHTTPURLResponse status description

I use a web service, and when something fails to verify, the error message is in the StatusDescription. I need a way to get this and display it to the user, but all I see in NSHTTPURLResponse is the StatusCode and a way to convert the status code into a standard error message.

The web server always returns a status code of 500 regardless of the data error, so I cannot get the problem out of the code.

+4
source share
3 answers

You may receive a standard error message:

[NSHTTPURLResponse localizedStringForStatusCode:httpResponse.statusCode]; 

Although there is no way to get the reason message from this response using the Foundation Framework if there is an additional message on your server. To do this, you should use the Core Foundation classes. The latter provides this ability:

CFStringRef myStatusLine = CFHTTPMessageCopyResponseStatusLine(myResponse);

+7
source

NSLog status is displayed here so that you turn off, as you can see, the code goes to didFailWithError.

 - (void)connection:(NSURLConnection *) connection didFailWithError:(NSError *)error { NSLog(@"Connection didFailWithError (connection %p) (code %d) %@ %@ %@", connection, [error code], [error localizedDescription], [error localizedFailureReason], [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); } 
0
source

I ended up using AFNetworking because the localized description did not return the error message provided by the server.

0
source

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


All Articles