Curl is a command that sends HTTP requests on Linux. Of course, Postman is a great tool for managing interface calls, but in the case of verifying that one Linux server is calling another Linux server API, the only thing to use is the curl command.

The following describes the common curl command scenarios. You can accumulate some o&M knowledge and apply it to your daily development work.

Open up a website

curl www.mynamecoder.comCopy the code

Send a POST request

curl -d "userName=xiaoming&password=coder1024" http://www.mynamecoder.com/loginCopy the code

Parameters that

-d: Specifies the parameters to be passedCopy the code

With the -d parameter, HTTP requests are automatically coded with a request header content-Type: application/x-www-form-urlencoded. The request is automatically converted to the POST method.

Send a GET request

curl -d "title=curl" http://www.mynamecoder.com/searchCopy the code

Parameters that

-g: indicates a GET request. By default, it is a POST requestCopy the code

Sends a POST request in JSON format

$ curl -d '{"userName": "xiaoming", "password": "123456"}' -H 'Content-Type: application/json' https://www.mynamecoder.com/loginCopy the code

Parameters that

-h: indicates the HTTP request headerCopy the code

Upload a file

curl -F '[email protected]' https://mynamecoder.com/uploadCopy the code

Parameters that

The above command adds the HTTP request header Content-Type: multipart/form-data and uploads the file photo. PNG as the file field.

The -f argument can specify the MIME type.

curl -F '[email protected]; type=image/png' https://mynamecoder.com/uploadCopy the code

Curl defaults the MIME type to application/octet-stream.

The -f argument can also specify a file name.

$ curl -F '[email protected]; filename=me.png' https://mynamecoder.com/uploadCopy the code

In the command above, the original file is named photo.png, but the file received by the server is named me.png.

Curl curl curl curl curl curl curl curl curl curl curl curl curl curl curl curl

conclusion

Xiao Ming is a little lazy recently. 2019 is coming to an end. In 2020, I will try to share my daily development every week!Welcome to follow the wechat public account “Programmer Xiaoming” for more resources.