Bootstrap FreeKB - Curl - Display a resource using the -X or --request GET option
Curl - Display a resource using the -X or --request GET option

Updated:   |  Curl articles

The curl command without any options will:

  • Display the contents of a file on a server, such as a web server or FTP server
  • Display the result of a GET request

 


Display the contents of a file on a server

In this example, the contents of the index.html file on the www.example.com web server is displayed.

~]# curl http://www.example.com/index.html
<html>
  <body>
    Hello World
  </body>
</html>

 

In this example, the contents of the foo.json file on the www.example.com web server is displayed.

~]# curl http://www.example.com/foo.json
[
 {
  "foo":"bar"
 }
]

 

Or, more realistically, most URL that return JSON are REST APIs, perhaps something like this.

~]# curl http://www.example.com/api/v1
[
 {
  "foo":"bar"
 }
]

 

By default, curl uses the GET method, thus there is no need to use the -X or --request option when using the GET method. The following commands will both return the content of the index.html page. The -X or --request option must be used when using any other method (POST, PUT, DELETE). 

curl http://www.example.com/index.html
curl -X GET http://www.example.com/index.html

 


--url

The --url option is used to define the URL being requested. This option is typically not needed, thus the following commands both display the contents of the index.html page from the www.example.com web server.

curl http://www.example.com/index.html
curl --url http://www.example.com/index.html

 


Display the result of a GET request

Let's say www.example.com contains index.php with following markup.

<?php echo $_GET['foo']; ?>

 

Entering the following URL in a web browser would display "bar".

http://www.example.com/index.php?foo=bar

 

Likewise, the following curl command would also display "bar".

curl http://www.example.com/index.php?foo=bar
. . .
bar

 


Download file

The -o or --output option can be used to redirect the content of the remote file to a file on your local system.

 


Public Certificate

The following command can be used to determine the public certificate that was requested from a particular URL.

curl --verbose --insecure https://www.example.com

 


Authentication

Let's say access to this page require basic authentication (username/password). The -u or --user option can be used to include a username and password in the request.

curl --user john.doe:itsasecret https://www.example.com/foo.html

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 3f9824 in the box below so that we can be sure you are a human.