How to use the command "httpie" (with examples)

How to use the command "httpie" (with examples)

  • Osx
  • December 25, 2023

HTTPie is a user-friendly command-line tool that makes it easy to make HTTP requests. It provides a simple and intuitive interface for interacting with HTTP servers. Whether you need to send GET or POST requests, download files, or follow redirects, HTTPie has got you covered.

Use case 1: Send a GET request (default method with no request data)

Code:

http https://example.com

Motivation: This command is used to send a GET request to the specified URL. In this example, we are sending a GET request to https://example.com.

Explanation:

  • http: The command name
  • https://example.com: The URL to which the GET request is sent

Example output:

HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Date: Wed, 04 May 2022 10:00:00 GMT
...

{response body}

Use case 2: Send a POST request (default method with request data)

Code:

http https://example.com hello=World

Motivation: This example demonstrates how to send a POST request to a URL with request data. In this case, we are sending a POST request to https://example.com with a request parameter hello set to World.

Explanation:

  • http: The command name
  • https://example.com: The URL to which the POST request is sent
  • hello=World: The request data, in this case, a query parameter named hello with the value World

Example output:

HTTP/1.1 200 OK
Content-Type: application/json
Date: Wed, 04 May 2022 10:00:00 GMT
...

{response body}

Use case 3: Send a POST request with redirected input

Code:

http https://example.com < file.json

Motivation: This use case shows how to send a POST request to a URL using redirected input. The request data is provided from the file file.json.

Explanation:

  • http: The command name
  • https://example.com: The URL to which the POST request is sent
  • < file.json: The input for the request is redirected from the file file.json

Example output:

HTTP/1.1 200 OK
Content-Type: application/json
Date: Wed, 04 May 2022 10:00:00 GMT
...

{response body}

Use case 4: Send a PUT request with a given JSON body

Code:

http PUT https://example.com/todos/7 hello=world

Motivation: This example demonstrates how to send a PUT request to a specified URL with a given JSON body. In this case, we are sending a PUT request to https://example.com/todos/7 with a JSON body containing a key-value pair hello with the value world.

Explanation:

  • http: The command name
  • PUT: The HTTP method of the request
  • https://example.com/todos/7: The URL to which the PUT request is sent
  • hello=world: The JSON body of the request, containing a key-value pair hello with the value world

Example output:

HTTP/1.1 200 OK
Content-Type: application/json
Date: Wed, 04 May 2022 10:00:00 GMT
...

{response body}

Use case 5: Send a DELETE request with a given request header

Code:

http DELETE https://example.com/todos/7 API-Key:foo

Motivation: This use case illustrates how to send a DELETE request to a URL with a specific request header. In this example, we are sending a DELETE request to https://example.com/todos/7 with a request header API-Key set to foo.

Explanation:

  • http: The command name
  • DELETE: The HTTP method of the request
  • https://example.com/todos/7: The URL to which the DELETE request is sent
  • API-Key:foo: The request header, specifying an API-Key with the value foo

Example output:

HTTP/1.1 204 No Content
Date: Wed, 04 May 2022 10:00:00 GMT
...

{empty response body}

Use case 6: Show the whole HTTP exchange (both request and response)

Code:

http -v https://example.com

Motivation: This example demonstrates how to view both the request and response of an HTTP exchange. The -v option is used to enable verbose output.

Explanation:

  • http: The command name
  • -v: The option to enable verbose output, which displays both the request and response

Example output:

GET / HTTP/1.1
Host: example.com
...

HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Date: Wed, 04 May 2022 10:00:00 GMT
...

{response body}

Use case 7: Download a file

Code:

http --download https://example.com

Motivation: This use case shows how to download a file from a URL. The --download option is used to specify that the content should be downloaded.

Explanation:

  • http: The command name
  • --download: The option to specify that the content should be downloaded

Example output:

Downloaded [1.23 KB]:
  -> example.html

Use case 8: Follow redirects and show intermediary requests and responses

Code:

http --follow --all https://example.com

Motivation: This example demonstrates how to follow redirects and show intermediary requests and responses. The --follow option is used to follow redirects, and the --all option is used to show all the requests and responses.

Explanation:

  • http: The command name
  • --follow: The option to follow redirects
  • --all: The option to show all the requests and responses

Example output:

1: GET / HTTP/1.1
   ...

2: GET /new-location HTTP/1.1
   ...

3: GET /final-location HTTP/1.1
   ...

HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Date: Wed, 04 May 2022 10:00:00 GMT
...

{response body}

Conclusion:

HTTPie is a versatile command-line tool that allows users to interact with HTTP servers effortlessly. With its user-friendly interface and various options, it simplifies the process of making HTTP requests, handling different methods, displaying verbose output, following redirects, and downloading files. Whether you are a developer debugging APIs or a sysadmin managing HTTP servers, HTTPie is a powerful tool to have in your toolkit.

Tags :

Related Posts

How to use the command 'tee' (with examples)

How to use the command 'tee' (with examples)

The tee command is a command-line utility that reads from standard input and writes to both standard output and files or commands.

Read More
How to use the command btop (with examples)

How to use the command btop (with examples)

btop is a resource monitor that provides information about the CPU, memory, disks, network, and processes.

Read More
How to use the command 'duc' (with examples)

How to use the command 'duc' (with examples)

The ‘duc’ command is a collection of tools for indexing, inspecting, and visualizing disk usage.

Read More