<< Back to the REST API POST example
What is JSON?
What is HTTP POST?
HTTP POST is one of the nine standard HTTP methods. The POST method requests that the REST API server accept the object enclosed in the body of the message at the endpoint identified by the Request-URI. The POST method differs from HTTP HEAD and GET requests in that HTTP POST requests can change the server's state.
What is REST API?
The REST API defines a set of rules for developers to create APIs:
- Client-server: the REST API client user interface (website, desktop or mobile application) should be decoupled from the REST API server (request processor and data storage) so that each part can be developed and scaled individually.
- Stateless: Every REST API request must be executed with all the necessary data, without assuming that the server might have any data from previous REST API client requests.
- Layered: the REST API client does not need to know if it communicates with an actual server or an intermediary. Intermediate servers (proxies or load balancers) can provide the underlying REST API server with additional scalability and security.
- Cacheable: each REST API response must be defined as cacheable or not.
In this REST API POST request example, the server informs the REST API client that it has returned JSON by sending Content-Type: application/json header in response.
Server response to our test REST API POST request.
What is the correct content type when posting JSON to a REST API endpoint?
The official MIME type for JSON is application/json. To POST JSON data to the server, you must specify the data type in the body of the POST message using the appropriate Content-Type request header.
Why is it important to specify the correct Content-Type when posting JSON to a REST API endpoint?
If you don't pass the correct Content-Type header to the server, your application may not work. The REST API server needs a valid Content-Type header to interpret the request message body data correctly. This is especially important for MVC frameworks that implicitly convert values from JSON to local variables.