§2023-02-27
3.2. curl Crash Course
A curl request is composed of the curl word, the URL you want to hit, and a set of options that allow you to modify anything you’d like in the request that will be sent.
Here are a few options we need to know to write our first requests:
-H: Shorthand for Header, this option lets us add or replace HTTP Header Fields. Example: -H "Content-Type: application/json" -d: Shorthand for data, this is the option we will use when we need to send data to the server. Example with a JSON payload: -d '{"name":"John Smith"}' -i, –include: When using this option, curl will not only display the body of the response sent back, but also the headers. -I, –head: This option tells curl to make a HEAD request which will only get the header of a document and not its body. -X, –request: This option specifies what kind of HTTP method we want to use in our request. The default is GET but we can use this option to send POST, PUT, PATCH or DELETE requests, for example.
curl -X ?? not supported, curl --help
curl -i http://localhost:4567/users
HTTP/1.1 200 OK
Content-Type: text/html;charset=utf-8
Content-Length: 203
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Connection: keep-alive
Server: thin
[ <-- the json has been modified to fit into a page
{"first_name":"Thibault","last_name":"Denizet","age":25,"id":"thibault"},
{"first_name":"Simon","last_name":"Random","age":26,"id":"simon"},
{"first_name":"John","last_name":"Smith","age":28,"id":"john"}
]
curl http://localhost:4567/users
[ <-- the json has been modified to fit into a page
{"first_name":"Thibault","last_name":"Denizet","age":25,"id":"thibault"},
{"first_name":"Simon","last_name":"Random","age":26,"id":"simon"},
{"first_name":"John","last_name":"Smith","age":28,"id":"john"}
]
curl -I http://localhost:4567/users
HTTP/1.1 200 OK
Content-Type: text/html;charset=utf-8
Content-Length: 203
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Connection: keep-alive
Server: thin
3.4. HTTP Response
What curl displayed for us is an HTTP response. This response can be divided into four areas:
-
The Start-Line (mandatory)
-
A list of Header Fields (can be 0 or more)
-
An Empty Line (mandatory)
-
A Message-Body (optional)
-
$ curl -v -i http://localhost:4567/users
* Trying ::1:4567... ----> snet
* Connected to localhost (::1) port 4567 (#0)
> GET /users HTTP/1.1
> Host: localhost:4567
> User-Agent: curl/7.74.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK <----- receiving
HTTP/1.1 200 OK
< Content-Type: text/html;charset=utf-8
Content-Type: text/html;charset=utf-8
< Content-Length: 203
Content-Length: 203
< X-XSS-Protection: 1; mode=block
X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
X-Frame-Options: SAMEORIGIN
< Connection: keep-alive
Connection: keep-alive
< Server: thin
Server: thin
<--- an empty line
<
* Connection #0 to host localhost left intact
[
{"first_name":"Thibault","last_name":"Denizet","age":25,"id":"thibault"},
{"first_name":"Simon","last_name":"Random","age":26,"id":"simon"},
{"first_name":"John","last_name":"Smith","age":28,"id":"john"}
]