JSON
JSON is the lingua franca of modern web services and it is also theimplicit content type HTTPie uses by default.
Simple example:
- $ http PUT example.org name=John email=[email protected]
- PUT / HTTP/1.1
- Accept: application/json, */*
- Accept-Encoding: gzip, deflate
- Content-Type: application/json
- Host: example.org
- {
- "name": "John",
- "email": "[email protected]"
- }
Default behaviour
If your command includes some data request items, they are serialized as a JSONobject by default. HTTPie also automatically sets the following headers,both of which can be overwritten:
Content-Type | application/json |
Accept | application/json, / |
Explicit JSON
You can use —json, -j
to explicitly set Accept
to application/json
regardless of whether you are sending data(it's a shortcut for setting the header via the usual header notation:http url Accept:'application/json, /'
). Additionally,HTTPie will try to detect JSON responses even when theContent-Type
is incorrectly text/plain
or unknown.
Non-string JSON fields
Non-string fields use the :=
separator, which allows you to embed raw JSONinto the resulting object. Text and raw JSON files can also be embedded intofields using [email protected]
and :[email protected]
:
- $ http PUT api.example.com/person/1 \
- name=John \
- age:=29 married:=false hobbies:='["http", "pies"]' \ # Raw JSON
- description=@about-john.txt \ # Embed text file
- bookmarks:=@bookmarks.json # Embed JSON file
- PUT /person/1 HTTP/1.1
- Accept: application/json, */*
- Content-Type: application/json
- Host: api.example.com
- {
- "age": 29,
- "hobbies": [
- "http",
- "pies"
- ],
- "description": "John is a nice guy who likes pies.",
- "married": false,
- "name": "John",
- "bookmarks": {
- "HTTPie": "https://httpie.org",
- }
- }
Please note that with this syntax the command gets unwieldy when sendingcomplex data. In that case it's always better to use redirected input:
- $ http POST api.example.com/person/1 < person.json