curl command - interface test

curl command - interface test

JUST DO IT

warm spring day

On the os represented by linux/Unix, test the backend and simulate connection requests will write scripts

Scenes:

Interface testing tools on Linux include ab, restClient, postman, etc. The most commonly used method is curl for simple testing

curl is a very convenient Rest client, which can easily complete the Rest API test, use curl to send Get/Post/Delete/Put to the http protocol, and can also carry headers to meet the specific conditions of Rest API requirements

Curl common parameters

-X/--request [GET|POST|PUT|DELETE|…] use the specified http method to issue http request

-H/--header set the header in the request

-i/--include display response header

-d/--data set http parameters

-v/--verbose output more information

-u/--user user account

-b/--cookie cookie file path to use cookie

The parameters of the linux command line, the same function often has two identical parameters, one is a relatively short parameter, and the other is a relatively long parameter

for example:

The parameters -X and --request are the same. curl -X POST http://www.example.com or curl --request POST http://www.example.com/ are exactly the same.

curl sends request parameters using

Set headers:

curl -i -H "Content-Type: application/json" http://www.baidu.com

Set HTTP parameters:

curl -X POST -d "param1:value1¶m2=value2" or -d "param1=value1" -d "param2=value2"

session authentication:

curl -X GET 'http://www.baidu.com/' --header 'sessionid:sessionid值'

Using cookies:

curl -i --header "Content-Type:application/json" -X GET -b ~/cookie.txt http://www.baidu.com

Test interface to upload files: We use -F "file=@__FILE_PATH__" to transfer files. If you want to see detailed request information, you can add -v parameter

curl -i -X POST -F 'file=@/User/uploadFile.txt' -H "token:abc123" -v

HTTP Basic Authentication:

curl -i -u username:password http://www.baidu.com/api/foo'

Knowledge point expansion:

curl post request, data can be in xml or json format, and local xml and json files can be sent

curl -H 'Content-Type:application/json' -X POST -d '{"name":"zhangsan"}' http://www.baidu.com/

curl -X POST -H 'content-type: application/json' -d /apps/jsonfile.json http://www.baidu.com/

curl -X POST -H 'content-type:application/xml' -d '<?xml version="1.0" encoding="UTF-8"?><name>zhangsan</name>' http://www.baidu.com/

You may find it annoying to use commands like this, but when you just need to simply test an interface and perform some lightweight operations, is it convenient to use a command in the terminal or open a graphical tool? In most cases, the server system does not have graphical tools installed.

JUST DO IT

Guess you like

Origin blog.csdn.net/nhb687095/article/details/131006387