Curl GET, POST, Parameters and JSON

May 29, 2019
REST API Testing

curl GET

curl http://www.mydomain.com/test

With Parameter

curl http://www.mydomain.com/test?name=Desmond

Multiple Parameters

curl "http://www.mydomain.com/test?name=Desmond&age=40"

Multiple Parameters with URL encoding

curl -G --data-urlencode "name=Desmond Lua" --data-urlencode "age=40" http://www.mydomain.com/test

curl POST

curl -X POST http://www.mydomain.com/test

With multiple parameters

curl --data "name=Desmond Lua&age=40" http://www.mydomain.com/test

With multiple seprated parameters

curl --data "name=Desmond Lua" --data "age=40" http://www.mydomain.com/test

Multipart upload file

curl --form "file1=@test.txt" http://www.mydomain.com/test

Multipart upload file with new filename

curl --form "file1=@test.txt;filename=newname.txt" http://www.mydomain.com/test

Multipart upload file with parameters

curl --form "file1=@test.txt" --form name="Desmond Lua" --form age=40 http://www.mydomain.com/test

curl JSON

curl -X POST -d '{"name": "Desmond Lua", "age": 40}' -H "Content-Type:application/json" http://www.mydomain.com/test

JSON parameters from file

curl -X POST -d @test.json -H "Content-Type: application/json" http://www.mydomain.com/test

Options

-v Verbose to observe HTTP headers

curl -v http://www.mydomain.com/test
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 65.254.242.180...
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to www.mydomain.com (65.254.242.180) port 80 (#0)
> GET /test HTTP/1.1
> Host: www.mydomain.com
> User-Agent: curl/7.47.0
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 301 Moved Permanently
< Location: https://www.mydomain.com/test
< Server: BigIP
* HTTP/1.0 connection set to keep alive!
< Connection: Keep-Alive
< Content-Length: 0
<
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
* Connection #0 to host www.mydomain.com left intact

-s Silient to slient the progress (% Total ...)

curl -s http://www.mydomain.com/test

Remove the following

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 65.254.242.180...
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to www.mydomain.com (65.254.242.180) port 80 (#0)

curl Alternatives

Refer Tools to Test REST API

References:

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.