Article directory
curl use
Send a POST request using curl
HTTP POST requests are usually used to submit data, and there are generally four common POST methods for submitting data. When doing web backend development, it is inevitable to send requests to debug the interface by yourself. This article is how to use the curl tool to perform POST requests in various ways.
1. application/x-www-form-urlencoded
The most common POST request, form form. It is very simple to use curl to make a request, an example is as follows:
curl -X POST -d "name=zhangsan" 127.0.0.1:80/api/getInfo
2. Multipart/form-data
Such requests typically involve file uploads. The backend handles this type of request a bit more complicated as well.
curl 127.0.0.1:80/api/multipart -F raw=@raw.data -F name=zhangsan
3. application/json
curl 127.0.0.1:80/api/json -XPOST -d '{"name":"zhangsan"}' --header "Content-Type : application/json"
This method is similar to application/x-www-form-urlencoded
the type of POST request, the value of the -d parameter is a JSON string, and there is one more Content-Type: application/json
to specify the format of the sent content. After the web backend parses, the obtained structures are all name=zhangsan
key-value pairs.
4. Text/xml file content as submitted data
If there is a lot of data to be submitted and it is inconvenient to write in the command line, then you can write the data content into a file and submit the data -d @filename
through . This is one way of using the -d parameter. But it is not the same thing as POST multipart/form-data
in uploading files. The @ symbol indicates that what follows is a file name, and the content of this file is to be read as an argument to -d.
// 创建数据文件 data.json
{
"name" : "zhangsan",
"age" : 18,
"habit" : ["sing", "swimming"]
}
An example request is as follows:
curl 127.0.0.1:80/api/json -XPOST -d @data.json --header "Content-Type : application/json"
If you want to use application/x-www-form-urlencoded
the method to submit, and the backend parses out the same data, then the -d parameter is like this, pay attention to the way of writing the array parameter .
// data.txt
name=zhangsan&age=18&habit[]=sing&habit[]=swimming
An example request is as follows:
curl 127.0.0.1:80/api/test -XPOST -d @data.txt
curl 127.0.0.1:80/api/test -XPOST -d 'name=zhangsan&age=18&habit[]=sing&habit[]=swimming'
curl request http results are saved to a file
curl --header "Content-type : application/json" "hostname:port/path" > ./result.json
curl set custom HEADER header
Curl is a powerful command-line tool that can pass information to and get data from a server over a network. Many transport protocols are supported, notably HTTP/HTTPS
and others such FTP/FTPS, RTSP, POP3/POP3S, SCP, IMAP/IMAPS
as . When you send an HTTP request to a URL using curl, a default HTTP header containing the necessary header fields (eg User-Agent, Host, Accept) is used.
In some HTTP requests, it is necessary to override the default HTTP headers or add custom header fields. To solve these problems, curl provides an easy way to take full control over the HTTP headers of outgoing HTTP requests. The required parameters are -H 或者 --header
. In order to define multiple HTTP header fields, the -H option can be specified multiple times in the curl command. Examples are as follows:
curl -H "host:220.181.38.149" -H "Accept-language:es" -H "Cookie:token=xxxx" www.baidu.com -v
Precautions:
- The header, colon and value quality check cannot have spaces
- The custom header needs to be added after the standard header.
For standard HTTP header fields such as "User-Agent", "Cookie", and "Host", there is usually another setting method. The curl command provides specific options to set against these fields:
- -A (or ––user-anget): Set the User-Agent field
- -b(or ––cookie) : set cookie field
- -e (or ––referer): set the Referer field
Examples are as follows, both ways are equivalent:
curl -H "User-Agent: brower" hostname
curl -A "brower" hostname