jsonnext.jsfetch

Differences between text/plain and application/json


The Question is that What is the difference between text/plain and application/json. And Which one is the better to use or is there a proper uses?

Recently, I fetch the POST method to the API Routes without setting headers in the NextJS. And it sets the Content-Type of the header of the fetch request as a text/plain. (it seems like default value is text/plain)

I usually have made header's Content-Type as a application/json. And as you know even we had stringified the body props, it parsed as a json format at the API Routes.

But with text/plain header, API Routes do not parse it.


Solution

  • The Content-Type header is a hint from a client or server of what's to come. You can send a JSON object with an text/plain content type, but it may confuse the server.

    I'm sure there are frameworks and APIs out there that just assume that anything incoming is JSON and don't warn you, but there are also plenty that will error with a 415 error because they expect JSON and you told them you were sending text/plain instead.

    text/plain itself is a format that's basically 'any text format without structure' and not used that much over the web. If you save a file with a .txt extension in notepad, that's text/plain.