(Server agnostic question, but I am interested if it differs depending on the server software.)

I was recently told that we needed to change back end requests from JSON.stringify(object) with a content type of text/plain to just object with a content type of application/json. I am all for this due to technical correctness of identifying the data.

However, the reasoning was that this would provide performance benefits by alleviating the need to JSON.parse the string that is received.

It is my understanding that no matter what content type you send, it is still a string being received by the server. In the world of 0's and 1's of data transmission, there is no distinction between a JSON object and a string. I believe that when the server receives a Content-Type application/json, it just calls JSON.parse before passing the data to your application -- meaning there is no performance benefit to using one over the other, because they are doing the same thing.

Is my interpretation correct, or is the server somehow able to handle one content type faster than the other?

  • There's probably no performance benefit, but sending JSON with a text/plain content type is what we call a code smell. – Michael Hampton Jul 20 '18 at 16:10

On the contrary, I see a reason for using text/plain !

If you are using CORS (cross origin request) and using application/json then the request will no longer be considered "simple request" and then the browser will issue a pre-flight request (using http OPTIONS) to check the CORS is allowed.

Using text/plain will enable CORS without the pre-flight request, making the response return faster.

| improve this answer | |

For a well written application, no, it shouldn't matter in terms of performance, especially considering that automatic deserialization of JSON should be doing exactly the same thing as the default behavior of manually deserializing it. It does kind of matter in terms of correctness though (JSON is not plain text, so you shouldn't be using text/plain as the content type).

That said, there is one case I can think of where it might matter. If you have a firewall that does deep packet inspection and tries some from of antivirus scanning on the content, and both the firewall and the AV software blindly trust the content type, it might be more efficient to send the data s text/plain. The likelihood of all of those criteria being met is essentially nil however, so you shouldn't code for it.

| improve this answer | |
  • I'm just curious, for my edification, in what way is JSON not plain text? – EBGreen Jul 20 '18 at 16:54
  • @EBGreen It's structured data and it may contain byte values not considered by the local system to be 'plain text'. – Austin Hemmelgarn Jul 20 '18 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.