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

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

The http command is a tool called HTTPie, which is a user-friendly HTTP client that aims to be easier to use than cURL. It provides a simple and intuitive command-line interface for making HTTP requests.

Use case 1: Download a URL to a file

Code:

http --download example.org

Motivation: This use case is useful when you want to download the content of a web page or file directly to your local machine.

Explanation: The --download option tells HTTPie to download the response content and save it to a file. The URL (example.org) specifies the location of the resource to be downloaded.

Example output: The response content from example.org will be downloaded and saved as a file.

Use case 2: Send form-encoded data

Code:

http --form example.org name='bob' profile_picture@'bob.png'

Motivation: This use case is useful when you need to send form-encoded data to an API endpoint, for example, when submitting a form on a website.

Explanation: The --form option tells HTTPie to send the data in form-encoded format. The key-value pairs in the command represent the form fields and their values. In this example, the name field is set to ‘bob’ and the profile_picture field is set to the contents of the file ‘bob.png’.

Example output: The command will send a POST request to example.org with the form-encoded data and display the response.

Use case 3: Send JSON object

Code:

http example.org name='bob'

Motivation: This use case is useful when you need to send JSON data to an API endpoint, for example, when making POST or PUT requests.

Explanation: In this example, the command sends a POST request to example.org with a JSON object in the request body. The key-value pair specifies the data to be sent, where name is the key and ‘bob’ is the value.

Example output: The command will send a POST request to example.org with the JSON data in the request body and display the response.

Use case 4: Specify an HTTP method

Code:

http HEAD example.org

Motivation: This use case is useful when you need to explicitly specify the HTTP method to be used for the request.

Explanation: In this example, the command sends a HEAD request to example.org, which only retrieves the headers of the response without the actual content.

Example output: The command will send a HEAD request to example.org and display the headers of the response.

Use case 5: Include an extra header

Code:

http example.org X-MyHeader:123

Motivation: This use case is useful when you need to include additional headers in the request, such as authentication credentials or custom headers.

Explanation: The command includes an extra header X-MyHeader with the value ‘123’ in the request to example.org. This header can be used to send additional information to the server.

Example output: The command will send a GET request to example.org with the additional header and display the response.

Use case 6: Pass a username and password for server authentication

Code:

http --auth username:password example.org

Motivation: This use case is useful when you need to authenticate yourself with the server using basic authentication.

Explanation: The --auth option tells HTTPie to include the specified username and password in the request headers for server authentication. In this example, the command sends a GET request to example.org with the provided username and password.

Example output: The command will send a GET request to example.org with the authenticated credentials and display the response.

Use case 7: Specify raw request body via stdin

Code:

cat data.txt | http PUT example.org

Motivation: This use case is useful when you want to send a raw request body, such as a file, using the stdin input.

Explanation: In this example, the command reads the contents of the file data.txt and sends it as the request body in a PUT request to example.org.

Example output: The command will send a PUT request to example.org with the raw request body from data.txt and display the response.

Conclusion:

The http command, powered by HTTPie, provides a simple and intuitive way to make HTTP requests from the command line. With its user-friendly interface and various options, it allows users to perform a wide range of HTTP operations, making it a powerful tool for interacting with APIs and testing web services.

Related Posts

Set-Date (with examples)

Set-Date (with examples)

Use Case 1: Add three days to the system date Set-Date -Date (Get-Date).

Read More
AWS EC2 Command Examples (with examples)

AWS EC2 Command Examples (with examples)

1. Display information about a specific instance aws ec2 describe-instances --instance-ids instance_id Motivation: This command allows you to retrieve detailed information about a specific EC2 instance.

Read More
Understanding the "jobs" Command (with examples)

Understanding the "jobs" Command (with examples)

The “jobs” command is a shell builtin command that is used to view information about processes spawned by the current shell.

Read More