Sending nested JSON object using HTTPie

后端 未结 3 834
再見小時候
再見小時候 2020-12-12 21:34

I am trying to use HTTPie to parse to send some nested JSON object, but I can not find how. It is pretty clear how to send a JSON object but not a nested one such as

相关标签:
3条回答
  • 2020-12-12 22:10

    You can pass the whole JSON via stdin:

    $ echo '{ "user": { "name": "john", "age": 10 } }' | http httpbin.org/post
    

    Or specify the raw JSON as value with :=:

    $ http httpbin.org/post user:='{"name": "john", "age": 10 }'
    
    0 讨论(0)
  • 2020-12-12 22:13

    I like this way:

    $ http PUT localhost:8080/user <<<'{ "user": { "name": "john", "age": 10 }}'
    

    It is preferrable because it has the same prefix as the related commands, and so it is convenient to find the commands with Ctrl+R in bash:

    $ http localhost:8080/user/all
    $ http GET localhost:8080/user/all # the same as the previous
    $ http DELETE localhost:8080/user/234
    

    If you have fishshell, which doesn't have Here Strings, I can propose the following workaround:

    ~> function tmp; set f (mktemp); echo $argv > "$f"; echo $f; end
    ~> http POST localhost:8080/user < (tmp '{ "user": { "name": "john", "age": 10 }}')
    
    0 讨论(0)
  • 2020-12-12 22:25

    Another approach mentioned in the httpie docs is using a JSON file; this has worked well for me for payloads that are more verbose and deeply nested.

    http POST httpbin.org/post < post.json
    
    0 讨论(0)
提交回复
热议问题