KnowledgeCollector
KnowledgeCollector

Reputation: 59

AFNetworking 2 empty POST response but no errors

It's a very tricky problem:

I do a POST request (login) to a server.

The server will answer:

Code:

func login (id: String, password: String){

    self.responseSerializer = AFJSONResponseSerializer()
    self.responseSerializer.acceptableContentTypes =  nil
    self.responseSerializer.acceptableStatusCodes = NSIndexSet(index: 400)
    //self.responseSerializer.acceptableStatusCodes = nil

    var param = ["id": lbid, "password": password]

    POST(APIURL.URL_LOGIN, parameters: param,

       { (operation : NSURLSessionDataTask!, response : AnyObject!) -> Void in
            //var finalResponse : Dictionary = Dictionary<String, String>()
            var tmp = response as String

            self.defaults.setObject(tmp, forKey: "USERSSID")
            self.defaults.setBool(true, forKey: "USERLOGGEDIN")

            println("Success login")

        }) { (operation : NSURLSessionDataTask!, error : NSError!) -> Void in

            println(error)
    }
}

It executes the failure blog and I get this error:

    Code=-1011 "Request failed: no error (200)" UserInfo=0x7f9fa1534760 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7f9fa15791e0> { URL: https://************ } { status code: 200, headers {
Connection = "Keep-Alive";
"Content-Length" = 107;
"Content-Type" = "application/json";
Date = "Wed, 04 Mar 2015 21:47:51 GMT";
"Keep-Alive" = "timeout=7, max=150";
Server = Apache;
"Set-Cookie" = "SID=************; expires=Mon, 02-Mar-2020 21:47:51 GMT; path=/;domain=.*********";}},NSErrorFailingURLKey=https://***********,com.alamofire.serialization.response.error.data=< CORRECT POST BODY>, NSLocalizedDescription=Request failed: no error (200)}

If I delete this code:

self.responseSerializer.acceptableStatusCodes = NSIndexSet(index: 400)

Then the app crashes. However the server responses with status code 200...

I don't know how to solve this issue. Could you help me? Here I get the correct body. But why not in the normal success blog?

EDIT:

    self.responseSerializer.acceptableStatusCodes = NSIndexSet(index: 200)

=> App crashs

    self.responseSerializer.acceptableStatusCodes = nil

=> App crashs

    self.responseSerializer.acceptableStatusCodes = NSIndexSet(index: 401)

=> App doesn't crash, but executes failure block. Status code in error message is 200 and error data contains the correct POST response body. => I could extract the message from the error data... but it's such a simple task. It has to work correctly.

Can't use Alamofire because I want to use ssl certificats!

Final edit: Don't no why, but the error disappeared by its own.

Upvotes: 1

Views: 1154

Answers (3)

Mathan
Mathan

Reputation: 31

The previous post was very useful to fix it. I was searching more and more but nothing was helped out. I tried of adding different codes and got tired ton's of times. Finally seen the above post, which was really solved the issue. I spent 2 days to find the solution. Initially got the error with -1011 with 400 error. I solved by using the below:

manager.responseSerializer.acceptableStatusCodes = [NSIndexSet indexSetWithIndex:400];

Again got different error like this: "200 no error". I solved by using below code:

manager.responseSerializer.acceptableStatusCodes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)];

It works!! cool!!

Hence, I advice you you guys to add the set of below code:

manager.responseSerializer.acceptableStatusCodes = [NSIndexSet indexSetWithIndex:400];
manager.responseSerializer.acceptableStatusCodes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)];

Thanks Stackoverflow!!!!

Upvotes: 0

Jeffrey Berthiaume
Jeffrey Berthiaume

Reputation: 4594

I found something that said:

acceptableStatusCodes

The acceptable HTTP status codes for responses. When non-nil, responses with status codes not contained by the set will result in an error during validation.

In Objective C:

self.responseSerializer.acceptableStatusCodes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)];

Upvotes: 0

Jeffrey Berthiaume
Jeffrey Berthiaume

Reputation: 4594

If you haven't already done so, check out Postman (a Google Chrome app). That's the best way to debug AFNetworking issues, by simulating the same request and making sure the data is coming through properly. A number of times, I've been fighting an issue to then use Postman and discover that it's something the server is doing.

Upvotes: 1

Related Questions