TooManyEduardos
TooManyEduardos

Reputation: 4404

AFNetworking 2.0 header Content-Type not being sent on POST call

I'm trying to convert some NSURLConnection code to AFNetworking 2.0 and I'm having issues with the POST calls. The GET calls work but not the POST.

Here's my code:

+(void)login:(User*)myUser
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:myUser.username password:myUser.password];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    [manager POST:URL_LOGIN
       parameters:nil
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
              NSLog(@"success! Response = %@", responseObject);
          } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              NSLog(@"failure. Error = %@", error);
          }];
}

And here's the error I get back from the server:

Error Domain=com.<server>.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x7fe043c43670 {com.<server>.serialization.response.error.response=<NSHTTPURLResponse: 0x7fe043d52850> { URL: http://<server>/login.htm;jsessionid=98399EA8F090BF310D1CF5FD3C992A21?login_error=1 } { status code: 200, headers {
"Content-Language" = en;
"Content-Length" = 2801;
"Content-Type" = "text/html;charset=ISO-8859-1";
Date = "Tue, 28 Apr 2015 15:30:21 GMT";
Server = "Apache-Coyote/1.1";

I have also tried to place the Content-Type explicitly as a param but it is not working either.

I have found a lot of suggestions online that say to either modify the server (which I can't) or to just set the manager.requestSerializer = [AFJSONRequestSerializer serializer]; which is obviously not working either.

Any suggestions on this would be highly appreciated. Also, as mentioned earlier, this communication with this Content-Type works on NSURLConnection with NSMutableURLRequest.

Thank you!

Upvotes: 2

Views: 328

Answers (1)

Rayfleck
Rayfleck

Reputation: 12106

Your server is sending you text/html whether you like it or not.

So you have to make this an acceptable content type in the request serializer.

You can either add it, like this:

  manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

or use a more general HTTP serializer, not a JSON serializer, like this:

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

Upvotes: 2

Related Questions