curl command - GET and POST

table of Contents


Brief introduction

curl is a command line tool, as the name suggests is the client's URL tool.

The tool is so powerful, as many as dozens of command-line arguments, fully comparable to the postman this type of graphical interface tools.

Documentation: https: //catonmat.net/cookbooks/curl

reference:

  • http://www.ruanyifeng.com/blog/2011/09/curl.html

  • https://www.ruanyifeng.com/blog/2019/09/curl-reference.html


GET

Send a GET request, and the results are printed out

curl https://catonmat.net

Send a GET request and response of the body to the output file

curl -o output.txt https://catonmat.net


POST

Sending an empty POST request

curl -X POST https://catonmat.net

POST request transmission parameters has

curl -d 'login=Queen&password=123' -X POST https://google.com/login

When a -dparameter passed in the form of data, automatically Content-Typeset to application/x-www-form-urlencodedthe same time when using -dthe time parameter, -X POSTit may be omitted.

-d Parameters can also be written separately:curl -d 'login=Queen' -d 'password' https://google.com/login

Transmission parameters may have a POST request redirection

curl -L -d 'tweet=hi' https://api.twitter.com/tweet

curlDefault does not support redirection, add -Lparameters can be explicitly requested any redirection.

POST request data with JSON

curl -d '{"login":"Queen","password":"123"}' -H 'Content-Type: application/json' https://google.com/login

Using the -dparameter data passed JSON, and must use -Hthe parameters specified explicitly Content-Typeasapplication/json

POST request data with XML

curl -d '<user><login>ann</login><password>123</password></user>' -H 'Content-Type: application/xml' https://google.com/login

Using -darguments passed xml data, and must use -Hthe parameters specified explicitly Content-Typeasapplication/xml

POST request with the plaintext data

curl -d 'hello world' -H 'Content_Type: text/plain' https://google.com/login

Using -dparameters passed in plain text data while using the -Hparameters specified display Content-Typeastext/plain

POST request with the data in a file

curl -d '@data.txt' https://google.com/login

Use -dparameter incoming data, which @later symbol indicating that the data from that file.

The explicit URL encoded POST data

curl --data-urlencode 'comment=hello 中国' https://google.com/login

Use -d, the default data has been considered POST URL encoding , but if the data does not pass encoding process, then you need to use --data-urlencodeafter the data has not been encoded URL encoding, and then send

POST a binary file

curl -F '[email protected]' https://google.com/profile

Use -Fparameters to force curlto send a multi-part form data, automatically Content-Typeset multipart/form-data, let the statement curlread pig.pngdata and upload it to https://google.com/profile, and name the filenewfile

Transmitting a binary data, and set its MIME type

curl -F '[email protected];type=iamge/png'

Use -Fparameter upload a binary file, and set the MIME type of the file to image/png, if not set, it defaults toapplication/octet-stream

MIME type (Multipurpose Internet Mail Extensions, media type) is a standard used to represent the nature and format of documents, files, and a stream of bytes.

Send binary data and change the file name

curl -F '[email protected];filename=cat.png' https://google.com/login

Like the previous two, the statement sent via a POST request binary data, and change the file name, so that the server can see it is not the original name pig.png, but a new name cat.png, and save it asnewfile

Guess you like

Origin www.cnblogs.com/leafs99/p/curl_get_post.html