LinuxCommandLibrary

http

Send HTTP requests from the command line

TLDR

Make a simple GET request (shows response headers and content)

$ http [https://example.com]
copy

Print specific parts of the content (H: request headers, B: request body, h: response headers, b: response body, m: response metadata)
$ http [[-p|--print]] [H|B|h|b|m|Hh|Hhb|...] [https://example.com]
copy

Specify the HTTP method when sending a request and use a proxy to intercept the request
$ http [GET|POST|HEAD|PUT|PATCH|DELETE|...] --proxy [http|https]:[http://localhost:8080|socks5://localhost:9050|...] [https://example.com]
copy

Follow any 3xx redirects and specify additional headers in a request
$ http [[-F|--follow]] [https://example.com] ['User-Agent: Mozilla/5.0' 'Accept-Encoding: gzip']
copy

Authenticate to a server using different authentication methods
$ http [[-a|--auth]] [username:password|token] [[-A|--auth-type]] [basic|digest|bearer] [GET|POST|...] [https://example.com/auth]
copy

Construct a request but do not send it (similar to a dry-run)
$ http --offline [GET|DELETE|...] [https://example.com]
copy

Use named sessions for persistent custom headers, auth credentials and cookies
$ http --session [session_name|path/to/session.json] [[-a|--auth]] [username]:[password] [https://example.com/auth] [API-KEY:xxx]
copy

Upload a file to a form (the example below assumes that the form field is )
$ http [[-f|--form]] [POST] [https://example.com/upload] [cv@path/to/file]
copy

SYNOPSIS

http [flags] [METHOD] URL [ITEM [ITEM]]

PARAMETERS

METHOD
    HTTP method to use (GET, POST, PUT, DELETE, PATCH, etc.). Defaults to GET if data is not provided, POST otherwise.

URL
    The URL to make the HTTP request to.

ITEM
    Request items to send (e.g., data, headers). Can be key=value for data, header:value for headers, or @filename for file upload. Use `=` to force string representation.

-h, --help
    Show help message and exit.

-v, --verbose
    Verbose mode. Prints the whole request and response.

-f, --follow
    Follow redirects.

-j, --json
    Serialize data items as a JSON object.

-a USER[:PASS], --auth USER[:PASS]
    Authenticate as USER with password PASS.

-A TOKEN, --auth-type TOKEN
    Authentication type (basic, digest, bearer).

DESCRIPTION

While there isn't a single, universally recognized "http" command built into all Linux distributions, this typically refers to a wrapper around `curl` or `wget` designed to make HTTP requests easier to formulate, especially for humans.

It simplifies common tasks like sending JSON data, setting headers, and authenticating with web services. Such a client often presents a more intuitive syntax than raw `curl` commands, making it easier to interact with REST APIs and other HTTP endpoints directly from the terminal. This greatly aids in development, debugging, and automating web interactions.

This command offers a simplified way to construct HTTP requests and analyze their responses. It streamlines sending POST requests with JSON, setting custom headers, and viewing the resulting HTTP headers and body.

CAVEATS

This command is not a standard Linux utility and requires installation of a specific HTTP client, like `httpie` or `curlie`. The specific flags and behaviors can vary depending on the particular implementation being used.

CONTENT TYPES

The `http` command intelligently sets the Content-Type header based on the data being sent. For example, when sending JSON data with the `-j` flag, it automatically sets the Content-Type to `application/json`.

FILE UPLOADS

The `@filename` syntax makes it easy to upload files as part of the HTTP request. It handles the appropriate multipart/form-data encoding.

EXIT CODES

The exit codes may vary depending on the HTTP tool but usually follow the following pattern: 0 for success, and non-zero for errors.

HISTORY

Tools like `httpie` and `curlie` emerged to address the verbosity and complexity of `curl` for common HTTP tasks. They aimed to provide a more user-friendly interface and better default behavior for interacting with APIs, especially RESTful services. These tools gained popularity among developers for testing and debugging web services directly from the command line.

SEE ALSO

curl(1), wget(1)

Copied to clipboard