What is HTTPie?
HTTPie is a command-line HTTP client, released under the BSD-3-Clause license and installed as the http and https commands, designed to make ad hoc API calls quicker to type and easier to read than curl. Its request-item syntax builds a JSON body from key=value pairs by default, and responses are formatted and syntax-highlighted. HTTPie, Inc. also publishes a separate graphical client for desktop and web, which is not open source and is labeled a public beta; the CLI is the long-standing core of the project.
Why use it in the API Lifecycle?
- Terse request syntax:
Header:Valuesets a header,field=valueadds a JSON string field,field:=123adds a raw JSON value (number, boolean, array, object),param==valueadds a query parameter, andfield@fileuploads a file with--formor--multipart. - JSON by default: Data fields are serialized as a JSON object with the matching
Content-TypeandAcceptheaders;--formswitches to form encoding. - Readable output: Formatted, colorized responses, with
--print(-p) selecting which parts to show: request headersH, request bodyB, response headersh, response bodyb. - Offline mode:
--offlinebuilds and prints the request without sending it. - Sessions:
--sessionpersists headers, auth and cookies across calls to the same host.
Best Practices
- Use
--check-statusin scripts. By default HTTPie exits 0 whatever the status code; with the flag it exits 3, 4 or 5 for a 3xx (without--follow), 4xx or 5xx response, so a500 Internal Server Errorfails the script. - Use
:=for anything that is not a string.count=5sends"5"andcount:=5sends5; the difference is exactly the kind of type mismatch an API should reject with400 Bad Requestor422 Unprocessable Content, so it is worth testing both. See Input Validation. - Use
--offlineto show a colleague or a bug report the exact request, headers and body, without hitting a production endpoint or needing credentials. - Use
-p Hhto see request and response headers without bodies when debugging caching, content negotiation or redirects, where the answer is usually inCache-Control,VaryorLocation. - Remember that
--sessionfiles are plain-text JSON containing whatever auth and cookies were used. Keep them out of repositories and shared directories, and prefer short-lived tokens. See Token Lifecycle. - Do not use
--verify=noto get past a TLS error on anything but a disposable local environment. Point--verifyat your internal CA bundle instead. - For repeatable test suites rather than interactive calls, use a runner with assertions such as Hurl.