3

I want to send POST request using HTTPie from Windows shell

JSON input Looks Like:

{
    "name": "pub1",
    "email": "support@pub1.com",
    "address": {
        "city": "new york",
        "pincode": 12345
    }
}

I have tried:

http -v POST http://127.0.0.1:8000/publication/ name=pub1 email=support@pub1.com 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.

Andrew
  • 8,322
  • 2
  • 47
  • 70
Ganesh Patil
  • 105
  • 1
  • 7
  • Did you tried to use simple quotes outside httpie JSON args ? `address:='{"city":"Delhi", "pincode":24215}'` – Pierre May 17 '21 at 09:56
  • 1
    Yes I tried with single quotes as well – Ganesh Patil May 17 '21 at 09:57
  • 1
    Easiest way would be to put it in a file and do `http :8000/publication/ @file.json`, if that works for you. Also, this question is purely about using `httpie` and has nothing to do with DRF, so maybe you can strip all that out and just keep the`httpie` part? – Andrew May 17 '21 at 10:37
  • 1
    Does this answer your question? [Sending nested JSON object using HTTPie](https://stackoverflow.com/questions/37215565/sending-nested-json-object-using-httpie) – Andrew May 17 '21 at 10:45
  • Thank You ! @AndrewBacker. I tried that solution in [https://stackoverflow.com/questions/37215565/sending-nested-json-object-using-httpie] link, But its not working out. I tried with single as well as double quotes and every possible combination. – Ganesh Patil May 17 '21 at 11:35
  • 1
    Try: `http -v :8000/publication/ name=pub1 email=support@pub1.com 'address:={"city":"new york", "pincode":12345}'` – Jakub Roztocil May 17 '21 at 12:34
  • I tried that too but didn't work.. @JakubRoztocil – Ganesh Patil May 18 '21 at 03:16

1 Answers1

5

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
Andrew
  • 8,322
  • 2
  • 47
  • 70