objective c - NSURLConnection returning error instead of response for 401 -


i have web api that, specific request returns status code 200 if went ok, , 401 if user not logged in based on authorization token. works fine if response status 200, doesn't seem work if response status 401, returning connection error code -1012, while response nil.

so, following code:

[nsurlconnection sendasynchronousrequest:request queue:queue completionhandler:^(nsurlresponse *response, nsdata *data, nserror *connectionerror) {     nslog(@"%@", response);     nslog(@"%@", connectionerror);      nshttpurlresponse *httpresponse = (nshttpurlresponse *) response;     int statuscode = (int)[httpresponse statuscode];     nslog(@"response status code: %d", statuscode); 

will display

2015-04-01 15:58:18.511 myproject[3618:694604] <nshttpurlresponse: 0x155facc0> { url: *some_url* } { status code: 200, headers {     "access-control-allow-headers" = "content-type, accept, x-requested-with";     "access-control-allow-methods" = "post, get, put, update, options";     "access-control-allow-origin" = "*";     connection = "keep-alive";     "content-type" = "application/json";     date = "wed, 01 apr 2015 12:58:14 gmt";     server = "wildfly 8";     "transfer-encoding" = identity;     "x-powered-by" = "undertow 1"; } } 2015-04-01 15:58:18.513 myproject[3618:694604] (null) 2015-04-01 15:58:18.513 myproject[3618:694604] response status code: 200 

if response status 200, while if status code 401, get:

2015-04-01 16:05:55.988 myproject[3633:695836] (null) 2015-04-01 16:05:55.992 myproject[3633:695836] error domain=nsurlerrordomain code=-1012 "the operation couldn’t completed. (nsurlerrordomain error -1012.)" userinfo=0x146137c0 {nserrorfailingurlkey=*some_url*, nserrorfailingurlstringkey=*some_url*, nsunderlyingerror=0x1459e6d0 "the operation couldn’t completed. (kcferrordomaincfnetwork error -1012.)"} 2015-04-01 16:05:55.992 myproject[3633:695836] response status code: 0 

if same request using postman or android device, status code 401 following headers(copied postman):

connection → keep-alive content-length → 30 content-type → application/json date → wed, 01 apr 2015 13:07:34 gmt server → wildfly 8 x-powered-by → undertow 1 

is there fix or maybe library give me accurate response status? searched bit -1012 error, couldn't find , don't want base on that.

edit: after bit of research found following statement on appl's documentation: "if authentication required in order download request, required credentials must specified part of url. if authentication fails, or credentials missing, connection attempt continue without credentials."

but how can know if error after 401 status? can appear after type of request?

in order 401 status code, think you'll need implement protocol nsurlconnectiondelegate , connection:didreceiveauthenticationchallenge:.

so, you'll need pass delegate, maybe using [nsurlconnection connectionwithrequest:request delegate:self].

and, if aren't trying implement authentication challenge, rather return 200 status code, different json content.

hope can help.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -