How to send JSON POST requests in cURL?
This article detailed the key steps of using the cURL command to simulate a JSON format POST request: specifying the request method as POST, setting the request header Content-Type to application/json, adding request body data...
The basic syntax of the cURL command is curl [options] [URL]
, where [options]
are optional parameters used to configure various options of the request, and [URL]
is the target URL to be requested. By entering different parameter options and URLs on the command line, we can perform various types of requests, such as simulating GET and POST requests. This article will introduce how to use the cURL command to simulate a JSON format POST request.
Understanding POST Requests
A POST request is a way of submitting data to a server, commonly used for sending form data and uploading files. In contrast, a GET request is typically used to retrieve data from a server. Compared to GET requests, POST requests are considered more secure and private because they place data in the request body rather than in the URL.
Steps to Send a JSON Format POST Request Using cURL
Let's go through a simple example. Suppose we need to submit an object form containing a username and password to the server. We can use the cURL command to simulate sending a JSON format POST request.
1.Specify the request method as POST
Use the -X POST
parameter on the command line to specify the request method as POST.
curl -X POST
2.Specify the data format type of the request body
Set the request header Content-Type
to application/json
using -H "Content-Type: application/json"
to inform the server that the data in the request body is in JSON format.
curl -X POST -H "Content-Type: application/json"
3.Add request body data
Specify the JSON data to be sent, including key-value pairs for the username and password.
curl -X POST -H "Content-Type: application/json" -d '{"username":"admin", "password":"123456"}'
Note: In the Windows command line, cURL has some issues with single and double quotes. To avoid this problem, you can wrap the entire JSON data in double quotes and escape the double quotes inside the properties using \
, as shown below:
curl -X POST -H "Content-Type: application/json" -d "{\"username\":\"admin\", \"password\":\"123456\"}"
4.Specify the URL of the request
Replace http://127.0.0.1:8000/login
with the actual URL to which you want to send the POST request.
curl -X POST -H "Content-Type: application/json" -d '{"username":"admin", "password":"123456"}' http://127.0.0.1:8000/login
For Windows, using escaped quotes:
curl -X POST -H "Content-Type: application/json" -d "{\"username\":\"admin\", \"password\":\"123456\"}" http://127.0.0.1:8000/login
5.Execute the request and obtain the response
Press the "Enter" key on the command line to execute the request. cURL will send a POST request to the specified URL, including the form data we specified. The server will process this request and return the corresponding response.
Summary
This article detailed the key steps of using the cURL command to simulate a JSON format POST request: specifying the request method as POST, setting the request header Content-Type
to application/json
, adding request body data, sending the required information in JSON format, specifying the target URL, and ensuring the request is sent to the correct server address.
Learn more:
- How to Decode (Visualize) Base64 Encoded Images in Postman?
- How to Encode a String to Base64 in Postman?
Learn more:
- How to Send GET Requests Using the cURL Command?
- How to Decode (Visualize) Base64 Encoded Images in Postman?