Summary of commonly used Linux curl HTTP commands

  In daily Java interface testing, Postman graphical interface tools are more commonly used, but Postman is very memory intensive.

  Curl is a commonly used command line tool. Its function is very powerful. There are dozens of command line parameters. Its name means the URL tool of the client. If you are proficient with the curl command, you can completely replace Postman for interface testing.

  For all the parameters of the curl command, please refer to " curl [options...] <url> options options ", this article briefly summarizes the meaning of some commonly used parameters, as well as the commonly used curl http request commands.

parameter effect
-A Specify the user agent header of the client, namely User-Agent.
The default user agent string for curl is curl/[version].
-b Send cookies to the server.
-d Specify the data to be sent.
- -data-urlencode -The -data-urlencode parameter is equivalent to -d, which sends the data of the POST request.
The difference is that the sent data will be automatically URL-encoded.
-H Add HTTP request headers.
-X Specify the HTTP request method.
Generally, optional fields are GET, POST, PUT, DELETE, etc.

  Examples of commonly used commands are as follows:

  1. curl sends an http get request to submit a single form data:
$ curl -H "Content-Type: application/x-www-form-urlencoded" -X GET "http://localhost:7778/test3?age=12"

$ curl -H "Content-Type: application/x-www-form-urlencoded" "http://localhost:7778/test3?age=12"

$ curl "http://localhost:7778/test3?age=12"

$ curl http://localhost:7778/test3?age=12

  The default Content-Type is application/x-www-form-urlencoded. When the form data is submitted, it will be submitted in GET mode by default. At this time, -X GET can also be defaulted.

  1. curl sends http get requests and submits multiple form data:
$ curl -H "Content-Type: application/x-www-form-urlencoded" -X GET "http://localhost:7778/test2?age=12&name=Lucy"

$ curl -H "Content-Type: application/x-www-form-urlencoded" "http://localhost:7778/test2?age=12&name=Lucy"

$ curl "http://localhost:7778/test2?age=12&name=Lucy"

$ curl http://localhost:7778/test2?age=12\&name=Lucy

  When submitting multiple form forms and the URL does not contain quotation marks, you need to add the escape character \, otherwise the parameters cannot be correctly recognized.

  1. Curl sends an http post request and submits a single form data:
$ curl -H "Content-Type: application/x-www-form-urlencoded" -X POST "http://localhost:7778/test4" -d 'age=12'

$ curl -H "Content-Type: application/x-www-form-urlencoded" -X POST "http://localhost:7778/test4?age=12"

$ curl -X POST "http://localhost:7778/test4?age=12"

$ curl -X POST http://localhost:7778/test4?age=12
  1. Curl sends http post requests and submits multiple form data:
$ curl -H "Content-Type: application/x-www-form-urlencoded" -X POST "http://localhost:7778/test1" -d 'age=12&name=Lucy'

$ curl -H "Content-Type: application/x-www-form-urlencoded" -X POST "http://localhost:7778/test1?age=12&name=Lucy"

$ curl -H "Content-Type: application/x-www-form-urlencoded" -X POST "http://localhost:7778/test1" -d 'age=12' -d 'name=Lucy'

$ curl -X POST "http://localhost:7778/test1?age=12&name=Lucy"

$ curl -X POST "http://localhost:7778/test1" -d 'age=12&name=Lucy'

$ curl -X POST http://localhost:7778/test1?age=12\&name=Lucy

  You can use the -d option multiple times to specify multiple transmission parameters, or use the -d option once to specify multiple parameters. When Content-Type is application/x-www-form-urlencoded, -X POST cannot be defaulted.

  1. curl sends http get request and submits json data:
$ curl -H "Content-Type: application/json" -X GET "http://localhost:7778/test6" -d '{"age":"12","name":"Lucy"}'

$ curl -H "Content-Type: application/json" -X GET "http://localhost:7778/test6" -d '{"age":"12"}'

  When the Content-Type is application/json, it is submitted by POST by default, and -X GET cannot be defaulted at this time.

  1. curl sends http post request and submits json data:
$ curl -H "Content-Type: application/json" -X POST "http://localhost:7778/test5" -d '{"age":"12","name":"Lucy"}'

$ curl -H "Content-Type: application/json" -X POST "http://localhost:7778/test5" -d '{"age":"12"}'

$ curl -H "Content-Type: application/json" "http://localhost:7778/test5" -d '{"age":"12","name":"Lucy"}'

  When the Content-Type is application/json, it is submitted by POST by default, and -X POST can be the default at this time.

  Article reference:

Guess you like

Origin blog.csdn.net/piaoranyuji/article/details/108641845