Reputation: 2583
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?
Upvotes: 31
Views: 94199
Reputation: 2183
curl --request POST \
--url http://host/api/content/ \
--header 'authorization: Basic Esdfkjhsdft4934hdfksjdf'
Upvotes: 6
Reputation: 9728
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
Upvotes: 59
Reputation: 29267
Don't use
$person = file_get_contents("php://input");
instead use
$person = $_POST['person'];
And if you're using curl from the command-line this is the syntax for wanting to POST json data:
curl -d 'person={"name":"bob"}'
Upvotes: 1