KJBweb

Technical Notes & How To's
Menu
  • Blog
  • How-To
Home
How-To
How to Perform a GET Request with CURL via the Command Line

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

Karl August 3, 2018

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.

Share
Tweet
Email
Prev Article
Next Article
Tags:bash curl

About The Author

Karl

Related Articles

Any computer needs to have accurate and accurate date and …

How to Check NTP Connectivity and Function on the Command Line

One Response

  1. moun

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

    October 7, 2020

Leave a Reply

Cancel reply

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

Recent Posts

  • How to Install 7zip on Ubuntu 18.04 LTS (Bionic Beaver)
  • How to Install the NewRelic MySQL Plugin
  • Repair “Repository Error: database disk image is malformed” YUM Issue
  • How to Check NTP Connectivity and Function on the Command Line
  • How to Perform a GET Request with CURL via the Command Line

Recent Comments

  • moun on How to Perform a GET Request with CURL via the Command Line
  • Karl on How to Install rar/unrar on Ubuntu 18.04 LTS (Bionic Beaver)
  • John on How to Install rar/unrar on Ubuntu 18.04 LTS (Bionic Beaver)
  • How to Install rar/unrar on Ubuntu 16.04 LTS (Xenial) - KJBweb on How to Install rar/unrar on Ubuntu 18.04 LTS (Bionic Beaver)
  • FC on How to Install rar/unrar on Ubuntu 16.04 LTS (Xenial)

Archives

  • February 2019
  • August 2018
  • November 2016

Categories

  • Blog
  • How-To

KJBweb

Technical Notes & How To's
Copyright © 2021 KJBweb
Hello Huginn