To post JSON to a REST API endpoint, you must send an HTTP POST (PUT or PATCH) request to the REST API server and provide JSON data in the body of the POST message. You also need to specify the data type in the body of the POST message using the Content-Type: application/json request header. In this REST API POST example, we also send the Accept: application/json request header to tell the REST API server that the API client expects JSON.
REST stands for Representative State Transfer and is a way of connecting two computer systems to each other over the Internet. The REST API is a browser and programming language agnostic, you can run your REST API client in any modern browser, or build one using any programming language.
The REST API defines a set of rules for developers to create APIs: 1. Client-server: the REST API user interface app (website or mobile app) should be decoupled from the REST API server (request processor and data storage) so that each part can be developed, and scaled individually. 2. Stateless: every REST API request must be made with all required data, and no assumption should be made that the server might have any data from previous REST API client requests. 3. Layered: the REST API client does not need to know if it is communicating with a real server or an intermediary. Intermediate servers (proxies or load balancers) can provide additional scalability and security to the underlying REST API server. 4. Cacheable: each REST API response must be defined as cacheable or not.
REST API POST Example
To send data to the REST API server, you must send an HTTP POST request and include the POST data in the body of the request. You also need to provide the Content-Type: application/json and Content-Length request headers. Below is an example of a REST API POST request to a ReqBin REST API endpoint.
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 in response.
The server response to our test REST API POST request.
REST API Server Response
HTTP/1.1 200 OK
What is the correct content type for 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 content type in the body of the 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 to the server, your application may not work. The REST API server needs a valid Content-Type to correctly interpret the data in the request message body. This is especially important for MVC frameworks that implicitly convert values from JSON to local variables.
If your REST API client is expecting JSON data from the server, it must also send the Accept: application/json request header. This request header tells the server that the client can accept and process JSON data.
The Content-Type: application/json response header informs the API client that the server has returned data in JSON format.