Reputation: 163
I'm consuming a webservice and when something fails for validation reasons the error message is in the StatusDescription. I need a way to get that and display it to the user but all I see on NSHTTPURLResponse is the StatusCode and a way to convert the status code to the standard error message.
The web server always returns a 500 status code no matter the data error so I can't infer the problem from the code.
Upvotes: 6
Views: 6438
Reputation: 163
I ended up changing over to using AFNetworking since the localized description wasn't returning the error message provided by the server.
Upvotes: 0
Reputation: 8372
You can obtain a standard error message like that:
[NSHTTPURLResponse localizedStringForStatusCode:httpResponse.statusCode];
Although there is no way to get the reason message from the given response using Foundation Framework if your server provides additional message there. You should use Core Foundation classes for that. The last provides such ability:
CFStringRef myStatusLine = CFHTTPMessageCopyResponseStatusLine(myResponse);
Upvotes: 7
Reputation: 6753
Here's an NSLog output of the status to start you off, as you can see, the code goes in 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]);
}
Upvotes: 0