Spam Me
Spam Me

Reputation: 33

Is Content Type in HTTP header validated before POST request from browser?

Let's say I received a HTTP(sent using JQuery) request on the backend with Content-Type header set as application/json. Is it guaranteed that the data will be json type? Do I need to check if the data is valid json before loading it in memory, I am using json library in Python.

Upvotes: 0

Views: 24

Answers (1)

Quentin
Quentin

Reputation: 943240

Is it guaranteed that the data will be json type?

No. Clients can lie about the content-type they are sending. Servers can lie about the content-type they are responding with.

Do I need to check if the data is valid json before loading it in memory, I am using json library in Python.

Probably not.

You do need to pay some attention to exception handling, but invalid JSON will probably cause your library to throw an exception and lead to a 500 Internal Server Error response (catching it and sending a 400 Bad Request would be neater though).

Upvotes: 1

Related Questions