HomecurlcURL - GET Request

cURL – GET Request

In this guide, we are going to discuss how to send a GET HTTP request using cURL with interactive examples.

1. HTTP GET Request in cURL

1. Http GET with cURL is very simple. To run cURL command you just need to run the curl command followed by http(s) URL, by default cURL consider as GET Http method.

curl https://jsonplaceholder.typicode.com/posts/2

Results:

{
  "userId": 1,
  "id": 2,
  "title": "qui est esse",
  "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
}

2. Also, you can use -X OR --request to run GET request in cURL.

curl -X GET https://jsonplaceholder.typicode.com/posts/2

Results:

{
  "userId": 1,
  "id": 2,
  "title": "qui est esse",
  "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
}

3. Run GET in verbose mode, has to include -v option.

curl -v https://jsonplaceholder.typicode.com/posts/2

Results:

*   Trying 172.67.131.170...
* TCP_NODELAY set
* Connected to jsonplaceholder.typicode.com (172.67.131.170) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, [no content] (0):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, [no content] (0):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use h2
* Server certificate:
*  subject: C=US; ST=California; L=San Francisco; O=Cloudflare, Inc.; CN=sni.cloudflaressl.com
*  start date: Jun  6 00:00:00 2022 GMT
*  expire date: Jun  5 23:59:59 2023 GMT
*  subjectAltName: host "jsonplaceholder.typicode.com" matched cert's "*.typicode.com"
*  issuer: C=US; O=Cloudflare, Inc.; CN=Cloudflare Inc ECC CA-3
*  SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* TLSv1.3 (OUT), TLS app data, [no content] (0):
* TLSv1.3 (OUT), TLS app data, [no content] (0):
* TLSv1.3 (OUT), TLS app data, [no content] (0):
* Using Stream ID: 1 (easy handle 0x5649fad39690)
* TLSv1.3 (OUT), TLS app data, [no content] (0):
> GET /posts/2 HTTP/2
> Host: jsonplaceholder.typicode.com
> User-Agent: curl/7.61.1
> Accept: */*
> 

* TLSv1.3 (IN), TLS handshake, [no content] (0):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* TLSv1.3 (IN), TLS app data, [no content] (0):
* Connection state changed (MAX_CONCURRENT_STREAMS == 256)!
* TLSv1.3 (OUT), TLS app data, [no content] (0):
* TLSv1.3 (IN), TLS app data, [no content] (0):
* TLSv1.3 (IN), TLS app data, [no content] (0):
< HTTP/2 200 
< date: Sun, 04 Sep 2022 02:17:30 GMT
< content-type: application/json; charset=utf-8
< content-length: 278
< x-powered-by: Express
< x-ratelimit-limit: 1000
< x-ratelimit-remaining: 999
< x-ratelimit-reset: 1660437986
< vary: Origin, Accept-Encoding
< access-control-allow-credentials: true
< cache-control: max-age=43200
< pragma: no-cache
< expires: -1
< x-content-type-options: nosniff
< etag: W/"116-jnDuMpjju89+9j7e0BqkdFsVRjs"
< via: 1.1 vegur
< cf-cache-status: HIT
< age: 6249
< accept-ranges: bytes
< report-to: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=ZOdhf9AR3xx6w1mGZmZY%2F%2FCTw6NjwY7h1lJOdhBhrUMbsUGVEOUTDpxx1ddG%2FkNVtDC9Owf0V3j8aM35Ne4JUpywXODaKef%2FfUrByOwc99RZBuaZM2aZeGd517MgFK1DwagC7p%2Br0CBhQ8aTjTtW"}],"group":"cf-nel","max_age":604800}
< nel: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
< server: cloudflare
< cf-ray: 74533aac98087ba9-LAX
< alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400
< 
{
  "userId": 1,
  "id": 2,
  "title": "qui est esse",
  "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
* TLSv1.3 (IN), TLS app data, [no content] (0):
* Connection #0 to host jsonplaceholder.typicode.com left intact
}

4. Passing Headers.

curl -H "Accept: application/json" https://jsonplaceholder.typicode.com/posts/2

                         OR

curl -header "Accept: application/json" https://jsonplaceholder.typicode.com/posts/2

Results:

{
  "userId": 1,
  "id": 2,
  "title": "qui est esse",
  "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
}

2. Conclusion

We have seen how to run HTTP GET request in cURL command with several interactive example.

3. Reference

  1. cURL JSON Pretty Print
  2. cURL Document
  3. Everything cURL

LEAVE A REPLY

Please enter your comment!
Please enter your name here