HTTPie is a powerful command-line HTTP client designed for easy interaction with HTTP servers. It provides a simple and intuitive interface, making it an excellent tool for developers, testers, and system administrators.
Basic Usage ¶
HTTPie follows a simple syntax: http [flags] [METHOD] URL [items].
1http GET https://api.example.com/posts
You can print the request without sending it by using the --offline
flag.
1http --offline https://api.example.com/posts
URL shortcuts for localhost
¶
HTTPie supports a curl-like shorthand for localhost. For instance, :3000
expands to http://localhost:3000
. If the port is omitted, it assumes port 80.
1http :/users # http://localhost/users
2http :5000/rss # http://localhost:5000/rss
Optional GET and POST ¶
If you don’t specify the METHOD, the HTTPie will use:
- GET for requests without body
- POST for requests with body
1http https://api.example.com/tags # GET tags
2http https://api.example.com/tags title="Tutorial" slug="tutorial" # POST a new tag
Querystring Parameters ¶
If you’re manually adding query string parameters in the terminal, try the
param==value
syntax. It avoids shell escaping for & separators and
automatically URL-escapes special characters in parameter names and values.
This differs from parameters in the full URL, which HTTPie doesn’t modify.
1http https://api.example.com/search q==httpie per_page==20
Sending Data ¶
You can send data in various formats such as JSON, form data, or files.
JSON Data ¶
1http POST https://api.example.com/posts title="Hello" body="World"
Form Data ¶
1http -f POST https://api.example.com/submit name=John email=john@example.com
Files ¶
1http --form POST https://api.example.com/upload file@/path/to/file.txt
Headers and Authentication ¶
HTTPie allows you to set headers and handle authentication easily.
Headers ¶
1http GET https://api.example.com/posts Authorization:"Bearer Token" User-Agent:"HTTPie"
Basic Authentication ¶
1http -a username:password GET https://api.example.com/protected
Bearer Authentication ¶
1https -A bearer -a token https://api.example.com/admin
Response Handling ¶
HTTPie provides various options for handling responses.
1http GET https://api.example.com/data Accept:application/json # Pretty Print JSON
2
3http GET https://api.example.com/image --output image.png # Save Response to File
4
5http --follow GET https://example.com # Follow Redirects