Curl/Bash code for REST API POST ExampleThis Curl/Bash code snippet was generated automatically for the REST API POST example.
<< 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?
REST stands for Representative State Transfer and is a way of connecting two computer systems 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 create a desktop or mobile REST API application using any programming language, including Curl/Bash.
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.
Curl/Bash REST API POST Example
To send data to the REST API server using Curl/Bash, you must make an HTTP POST request and include the POST data in the request's body. 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 API endpoint. Click on Generate Code to see the Curl/Bash code for this REST API POST example.
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.
If your Curl/Bash REST API client expects JSON data from the server, it must also send the Accept: application/json request header. The Accept header tells the server that the client can accept and process JSON data. If the server returns data in JSON format, it must inform the Curl/Bash API client of the data type using the Content-Type: application/json response header.