Bartłomiej Semańczyk Bartłomiej Semańczyk - 4 months ago 22x
Swift Question

Difference between AFJSONRequestSerializer and AFHTTPRequestSerializer

What actually is the reason that my request works for

and doesnt for `

I have set


and it doesnt work, but should.

I just send simple dictionary:

"approval_required" = 1;
birthdate = "2016-03-11";
gender = male;
name = Bartolo;
password = password1234;
username = "super user";

Here is similar question, but there is no explanation WHY it is like that.

Can anyone explain?

Rob Rob

The acceptableContentTypes has little to do with the format of a request, ie whether it is HTTP request or JSON request. A request serializer's fundamental responsibility is to create a request in the specified format. Thus, the choice of requestSerializer dictates whether you are creating a HTTP request (e.g. a Content-Type of application/x-www-form-urlencoded) or JSON request (e.g. a Content-Type of application/json). The acceptableContentTypes merely allows the request to specify in what format(s) your app will accept responses. (And, frankly, you shouldn't specify acceptableContentTypes, but rather just use the correct responseSerializer.) But the request format and the accepted response format are two completely different things. In fact, it's not uncommon to have a web service that only accepts HTTP requests, and only provides JSON responses.

In your case, it sounds like you have a web service that is expecting a JSON request. When you specify AFHTTPRequestSerializer, the request will not be in JSON format and will likely not be understood. And specifying acceptableContentTypes does not alter the fact that the request, itself, isn't JSON.

By the way, tools like Charles or Wireshark are very useful for debugging network code, and would be illuminating in diagnosing the difference in these two types of requests. If you use one of these tools to watch the raw requests/responses, the difference will jump out at you.