3
votes

I'm using AFNetworking to handle a reset password request from my mobile app to my rails server.

The api is returning: head :ok (results in a 200)

However, this is causing AFNetworking to run the failure block when I issue a getPath request.

I can do two things to have the success block run:

  1. have the api return head :no_content (results in a 204)
  2. don't set my Accept header to `application/json'

It seems like AFNetworking is expecting at least an empty array when the status code is 200 and the Accept header is application/json.

I don't have full control over the api, so is it possible to have a 200 with no content still trigger my success block, or is the 204 supposed to be used for this exact situation, where it's successful but nothing will be returned from the server?

Thanks!

1
Dang, I just hit this exact same use case also and cannot find anything mentioned anywhere else on the nets.raidfive

1 Answers

2
votes

Instead of using AFNetworking's success and completion blocks you could use AFHTTPRequestOperation and deal with the response codes yourself. For example:

// Setup HTTP client
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://website.com"]];

// Create the request
NSURLRequest *request = [client requestWithMethod:@"GET" path:@"authenticate" parameters:params];

// Create an HTTP operation with your request
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

// Setup the completion block
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    switch (operation.response.statusCode) {
        case 200:
            // Do stuff
            break;
        default:
            break;
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    switch (operation.response.statusCode) {
        case 400:
            // Do stuff
            break;
        default:
            break;
    }
}];

// Begin your request
[operation start];