Redirected Input
The universal method for passing request data is through redirected stdin
(standard input)—piping. Such data is buffered and then with no furtherprocessing used as the request body. There are multiple useful ways to usepiping:
Redirect from a file:
- $ http PUT example.com/person/1 X-API-Token:123 < person.json
Or the output of another program:
- $ grep '401 Unauthorized' /var/log/httpd/error_log | http POST example.org/intruders
You can use echo
for simple data:
- $ echo '{"name": "John"}' | http PATCH example.com/person/1 X-API-Token:123
You can also use a Bash here string:
- $ http example.com/ <<<'{"name": "John"}'
You can even pipe web services together using HTTPie:
- $ http GET https://api.github.com/repos/jakubroztocil/httpie | http POST httpbin.org/post
You can use cat
to enter multiline data on the terminal:
- $ cat | http POST example.com
- <paste>
- ^D
- $ cat | http POST example.com/todos Content-Type:text/plain
- - buy milk
- - call parents
- ^D
On OS X, you can send the contents of the clipboard with pbpaste
:
- $ pbpaste | http PUT example.com
Passing data through stdin
cannot be combined with data fields specifiedon the command line:
- $ echo 'data' | http POST example.org more=data # This is invalid
To prevent HTTPie from reading stdin
data you can use the—ignore-stdin
option.
Request data from a filename
An alternative to redirected stdin
is specifying a filename (as@/path/to/file
) whose content is used as if it came from stdin
.
It has the advantage that the Content-Type
header is automatically set to the appropriate value based on thefilename extension. For example, the following request sends theverbatim contents of that XML file with Content-Type: application/xml
:
- $ http PUT httpbin.org/put @/data/file.xml