jsonhttpcurlpost

POST JSON over CURL with basic authentication


I am using Curl from the command line to debug a small web API I am working on. The web API expects basic authentication and a JSON object as input (POST). Currently, this basic authentication works fine:

curl -i --user validuser:70e12a10-83c7-11e0-9d78-0800200c9a65 http://example.com/api.php

but I also want to send a JSON object as a POST request:

curl -i --user validuser:70e12a10-83c7-11e0-9d78-0800200c9a65 -X POST -d '{"person":{"name":"bob"}}' http://example.com/api.php

I'm getting a 400 Bad Request response with the above command, any ideas on how I bundle a JSON object in this POST request?


Solution

  • Try it with:

    curl -i --user validuser:70e12a10-83c7-11e0-9d78-0800200c9a65 -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"person":{"name":"bob"}}' http://mysite.com/api.php
    

    I've removed the json= bit in the body content.

    Alternatively, this post might be helpful: How to post JSON to PHP with curl