2

In my iPhone application I am making some web service calls using JSON. In the return data i am getting a status code (200, 404 etc), along with that they are sending a message.

Here is the result what I got in Rest client:

Status Code: 401 Invalid Access token
Cache-Control: private
Connection: Keep-Alive, Proxy-Support
Content-Length: 0
Date: Wed, 13 Mar 2013 06:37:26 GMT
Proxy-Support: Session-Based-Authentication
Server: Microsoft-IIS/7.5
Via: 1.1 WIN-JO3AMACI965
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET

In this, I want to get the message "Invalid Access token" along with the status code 401. Is it possible?

Thanks in advance.

Mithuzz
  • 1,091
  • 14
  • 43
  • What are you using to do your networking - `NSURLConnection`, or a library such as `RestKit` or `AFNetworking`? – lxt Mar 13 '13 at 09:59

1 Answers1

1

Assuming you're using NSURLConnection to send the request, you can get the status code from the response:

NSError* error = nil;
NSHTTPURLResponse* response = nil;

NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
int statusCode = response.statusCode;
NSString* statusText = [NSHTTPURLResponse localizedStringForStatusCode:statusCode];

If you're not using a synchronous request, you can use the delegate methods as described here.

Hope that helped.

Community
  • 1
  • 1
Ran Dahan
  • 415
  • 1
  • 3
  • 10
  • I tried as you said, but the message that I am getting is not as shown in the question. I am getting some other messages like, 'No error', 'server error' like that. And as per question i need to get the message 'Invalid Access token'. This is a custom message created at the server. – Mithuzz Mar 13 '13 at 10:20
  • It doesn't seem like `NSHTTPURLResponse` provides what you ask for.. If you are in control of the server, have you considered placing the status text in a custom header? That way, maybe you can get it with `response.allHeaderFields`. – Ran Dahan Mar 13 '13 at 10:29
  • Me too wasn't so sure that it would be possible or not, I just wanted to confirm it. Server team told me to retrieve it, thats why I asked. Anyway thanks :) – Mithuzz Mar 13 '13 at 10:35