JSON

JSON is the lingua franca of modern web services and it is also theimplicit content type HTTPie uses by default.

Simple example:

  1. $ http PUT example.org name=John email=[email protected]
  1. PUT / HTTP/1.1
  2. Accept: application/json, */*
  3. Accept-Encoding: gzip, deflate
  4. Content-Type: application/json
  5. Host: example.org
  6.  
  7. {
  8. "name": "John",
  9. "email": "[email protected]"
  10. }

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-Typeapplication/json
Acceptapplication/json, /

Explicit JSON

You can use —json, -j to explicitly set Acceptto 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]:

  1. $ http PUT api.example.com/person/1 \
  2. name=John \
  3. age:=29 married:=false hobbies:='["http", "pies"]' \ # Raw JSON
  4. description=@about-john.txt \ # Embed text file
  5. bookmarks:=@bookmarks.json # Embed JSON file
  1. PUT /person/1 HTTP/1.1
  2. Accept: application/json, */*
  3. Content-Type: application/json
  4. Host: api.example.com
  5.  
  6. {
  7. "age": 29,
  8. "hobbies": [
  9. "http",
  10. "pies"
  11. ],
  12. "description": "John is a nice guy who likes pies.",
  13. "married": false,
  14. "name": "John",
  15. "bookmarks": {
  16. "HTTPie": "https://httpie.org",
  17. }
  18. }

Please note that with this syntax the command gets unwieldy when sendingcomplex data. In that case it's always better to use redirected input:

  1. $ http POST api.example.com/person/1 < person.json