Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 authentications 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?

like image 644
2bard Avatar asked May 24 '11 10:05

2bard


People also ask

How do I post JSON string with basic authentication?

To send basic authentication credentials to the server, you need to convert the "username: password" pair to a Base64 encoded string and pass it in the authorization request header. Where: Authorization: standard HTTP authorization header. Basic: indicates HTTP Authorization type.

How pass JSON data in Curl Post?

To post JSON data using Curl, you need to set the Content-Type of your request to application/json and pass the JSON data with the -d command line parameter. The JSON content type is set using the -H "Content-Type: application/json" command line parameter. JSON data is passed as a string.

How do I send basic auth credentials with Curl?

To send basic auth credentials with Curl, use the "-u login: password" command-line option. Curl automatically converts the login: password pair into a Base64-encoded string and adds the "Authorization: Basic [token]" header to the request.

How do you add basic authentication to Curl?

To use basic authentication, use the cURL --user option followed by your company name and user name as the value. cURL will then prompt you for your password.


1 Answers

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

like image 107
Jits Avatar answered Sep 24 '22 19:09

Jits