How to Perform a GET Request with CURL via the Command Line

This post is part of a wider series on CURL, a very useful tools utilised extensively either straight from the command line, or through scripting language specific variants.

GET is one of the most commonly used HTTP methods, and is used to request data from a specified URL.

When making a GET request, remember that the query string (as in, the key/value pairs) make up the URI  of the request itself:

http://example-endpoint.com/people/input_form.php?firstname=John&surname=Doe

Also, remember:

  • GET requests can be cached
  • GET requests remain in the browser history
  • GET requests can be bookmarked
  • GET requests should never be used when dealing with sensitive data
  • GET requests have length restrictions
  • GET requests is only used to request data (not modify)

With that in mind, let’s see how it’s done and run through a couple of ways to deal with the result too.

Perform a GET Request with CURL on the Command Line

Performing a GET request is simple with CURL, simply have CURL hit the URL you wish with no additional flags or parameters:

[email protected]:~$ curl https://httpbin.org/get
{
"args": {},
"headers": {
"Accept": "*/*",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "curl/7.58.0"
},
"origin": "89.56.112.103",
"url": "https://httpbin.org/get"
}
[email protected]:~$

Here you can see I sent a GET request to httpbin.org, and got a response telling me a bit about myself such as my User-Agent, IP, etc…

Save CURL GET Request Response to File on the Command Line

Perhaps you want to save the contents of the servers response to a file for use, you can easily do this using CURL’s “-o” flag like this:

[email protected]:~$ curl -o response.json https://httpbin.org/get
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 214 100 214 0 0 329 0 --:--:-- --:--:-- --:--:-- 328

I called the file “response.json” because I knew the server was going to deliver a JSON response back to me, if you’re expecting an XML response, or something else, then name your file appropriately.

I can now open the file and see it’s content like so:

[email protected]:~$ cat response.json
{
"args": {},
"headers": {
"Accept": "*/*",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "curl/7.58.0"
},
"origin": "89.56.112.103",
"url": "https://httpbin.org/get"
}

Retrieve Only Response Code of a GET Request with CURL on the Command Line

If you aren’t interested in the contents of the GET request, though only want to see the response code (to see if the endpoint is up and working for example),  you can use the following:

[email protected]:~$ curl -s -o /dev/null -w "%{http_code}" https://httpbin.org/get
200

A 200 response indicates the request was successful, whereas a 404 means the endpoint hasn’t been found, or a 500 would suggest there’s an issue at the servers side.

Be sure to read through the other posts on CURL in this series to get to grips with this powerful tool.

1 thought on “How to Perform a GET Request with CURL via the Command Line

  • October 7, 2020 at 7:10 am
    Permalink

    hi
    what you do with the query string is not the same as your curl example, i see no firstname and surname

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.