cURL send JSON as x-www-form-urlencoded

55,266

Solution 1

For application/x-www-form-urlencoded you could try:

curl -d "param1=value1¶m2=value2" -X POST http://localhost:3000/blahblah

Where param1=value... have to be your JSON data as chicago=123&boston=245

Or explicit form:

curl -d "param1=value1¶m2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/blahblah

Instead of http://localhost:3000/blahblah you should provide real URL of your service.

Solution 2

The whole point of curl -F, according to the man page, is "to POST data using the Content-Type multipart/form-data according to RFC 2388." In other words, it's best used when you need to emulate an HTML form with a file input.

Instead use curl -d to specify the raw POST data:

curl -d '{"cities":{"chicago":123,"boston":245}}' https://example.com

If this is actually how they expect data, it is a misconfigured server, as x-www-form-urlencoded data should be in the form key=value.

Share:
55,266

Related videos on Youtube

Kousha
Author by

Kousha

Biomedical Engineering Master student at UBC. I develop apps for both fun and large scale application for clients.

Updated on July 28, 2022

Comments

  • Kousha
    Kousha 4 months

    I want to post the following JSON:

    {
       "cities": {
           "chicago": 123,
           "boston": 245
       }
    }
    

    Using curl as x-www-form-urlencoded without using a .json file. I cannot figure out how to build the curl -F ...

    • anubhava
      anubhava about 4 years
      curl -F 'data={"cities":{"chicago":123,"boston":245}}' ...
    • chepner
      chepner about 4 years
      What name/value pairs does the remote end expect? There's no one input that particular JSON maps to.