Ganesh Patil
Ganesh Patil

Reputation: 103

How to send POST request for Nested JSON using HTTPie?

I want to send POST request using HTTPie from Windows shell

JSON input Looks Like:

{
    "name": "pub1",
    "email": "[email protected]",
    "address": {
        "city": "new york",
        "pincode": 12345
    }
}

I have tried:

http -v POST http://127.0.0.1:8000/publication/ name=pub1 [email protected] address:="{"city":"new york", "pincode":12345}"

It Gives Following Error:

http: error: "address:={city: new": Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

httpie json error

I did this using Postman and Its Working, But I want to know How this can be done using httpie?

I also tried available solutions on SOF and GitHub, But Couldn't Figure out what is the problem.

Upvotes: 3

Views: 2772

Answers (1)

Andrew
Andrew

Reputation: 8674

Windows shell quoting rules are different, so you can't use :='<json>' with single quotes as all the osx/linux examples do, you need double quotes.

The error message that you get says "Expecting property name enclosed in double quotes", but thats confusing since it is in double quotes to the naked eye.

Escaping the the double quotes inside the json literal will fix this. You can do this by doubling up the quote character, as "".

"city" => ""city""

http -v post https://postman-echo.com/post address:="{""city"":""london""}"

POST /post HTTP/1.1
Content-Type: application/json
Host: postman-echo.com
User-Agent: HTTPie/2.3.0
{
    "address": {
        "city": "london"
    }
}

You can also use the echo trick to avoid all the quoting, if you prefer. This method is similar to using a file, so you specify the whole json document and not individual fields.

echo {"address": {"city":"london"} } | http -v post https://postman-echo.com/post

Upvotes: 5

Related Questions