curl cheat sheet

curl cheat sheet

The -X option specifies the HTTP method. The HTTP methods to be specified are as follows

HTTP Methods
GET
PUT
POST
DELETE
PATCH

You can pass a request body by specifying the -d option followed by a string. The request body is passed as a string.

curl -X POST -d '{"key":["aaa","bbb"]}' https://example.com/

The -I option displays response headers.

curl -I -X POST https://example.com/

The -i option displays response headers and response body.

curl -X POST -d '{"key":["aaa","bbb"]}' https://example.com/ -i

Pass request headers with the -H option. The following passes Host:xxx.execute-api.ap-northeast-1.amazonaws.com.

curl -X POST -H Host:xxx.execute-api.ap-northeast-1.amazonaws.com https://example.com/

To pass multiple request headers, use multiple -H options, such as -H 'xxx: yyy' -H 'aaa: bbb'.

Passing request headers with curl

To pass request headers, use the -H option, such as -H ‘Bearer hogehoge’.

curl -0 -i -X GET -H 'Authorization: Bearer hoge' https://example.com/company/1

Bearer Certification:「https://qiita.com/h_tyokinuhata/items/ab8e0337085997be04b1

Benchmarking with curl

You can use the -w option in curl for benchmark testing.

curl -X GET https://example.com/ -o /dev/null -w "%{time_total}\"

Hide the response body with -o /dev/null.

The variables available are (there are many more)

variable meaning
time_total Time to complete download (seconds)
time_starttransfer Time (in seconds) to receive the first byte of the response

Put a new line at the end of the response body

To put a newline code at the end of the curl command, use -w “\n” to put a newline code and make it easier to read.

Specify multiple query strings

If you specify multiple query strings, such as https://example.com/?hoge1=fuga1\&hoge2=fuga2, you need to escape the & from the & mark.

curl -X GET https://example.com/?hoge1=fuga1&hoge2=fuga2

Content-Type:application/x-www-form-urlencoded when request header is unspecified in curl

If you do not specify the request header in the curl command, it will be Content-Type:application/x-www-form-urlencoded.

curl --verbose https://~~/ > /dev/null

You can see that the Content-Type of the request header is application/x-www-form-urlencoded.

To prevent this, you need to explicitly set -H ‘ContentType:xxx’.

Specify HTTP1.0, HTTP1.1, HTTP2.0

The curl -0 option sends the request with HTTP1.0.

The curl –http1.1 option sends a request with HTTP1.1.

curl –http2 option sends a request with HTTP2.0.

Specify the request body with a heapdocument (Linux)

The request body is specified with the -d option. Then, the request body can be specified in a heared document with @- and EOF.

$ curl -X POST https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/v1/get -d @- <<EOF
> {
> key:value
> }
> EOF

Running curl commands without a proxy

In some cases, a proxy may get in the way and prevent curl commands from being executed. In such cases, use the --noproxy option to specify a host that does not use a proxy.

curl --noproxy localhost -X GET http://localhost:8080/~~~

curl to https://~~~ with curl in Windows 10

Why can't cURL properly verify a certificate on Windows?
When I try to use Curl on windows, to retrieve an https url, I get the dreaded "connection error (60)." The exact error ...

I thought curl on Windows 10 does not support the https protocol, but if I enclose the URL in double quotes, it works fine.

SSL Client Authentication

For SSL client authentication with curl, specify a .pem file. .pfx files do not seem to be supported.

curl --cert hoge.pem ~

If it is in pfx format, it must be converted to pem format using the following openssl command.

openssl pkcs12 -in ./hoge.pfx -out hoge.pem -nodes -clcerts

URL encoding

Here is an example of encoding query parameters with the curl command in Windows 10.

Use the “-G” and “–data-urlencode” options.

Hyphens and other characters must also be URL encoded.

curl -G -X GET http://localhost:8080/test --data-urlencode "hoge1-id=fuga1" --data-urlencode "hoge2-id=fuga2"

Copy the curl command from Chrome Developer Tools

You can access the appropriate URL in Chrome and copy the curl command (both win and bash) from the Network tab of the Chrome Developer Tools.

curlチートシート

JSON parse error: Invalid UTF-8 ~(Windows)

If you include Japanese characters in the request body using the curl command on Windows, you will get a “JSON parse error: Invalid UTF-8 start byte ~” error.

If you want to include Japanese characters in the request body using the curl command on Windows, prepare a json file.

Specify the file with the “-d” option and “@”.

curl -X POST -d @sample.json http://localhost:8080/hoge

sample.json

{"address":"大阪市"}

Sending local files with curl POST

Use the “-F” option to send local files with curl.

curl -X POST -F file1=@/path/to/file.json https://~

The file is specified in the form @filename.

コメント

Discover more from 株式会社CONFRAGE ITソリューション事業部

Subscribe now to keep reading and get access to the full archive.

Continue reading

Copied title and URL