Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl json post request via terminal to a rails app

I'm trying to create a user on my rails app with a curl command from os x terminal. No matter how I format the data, the app returns a responses that non of my validations have passed.

curl http://localhost:3000/api/1/users.json -i -X POST -d {"user":{"first_name":"firstname","last_name":"lastname","email":"[email protected]","password":"app123","password_confirmation":"app123"}}" 

I've tried every variation. I've tried using [] brackets, I've tried user={data..} and nothing seems to work. Any ideas?

like image 450
chris sun Avatar asked Apr 14 '11 04:04

chris sun


People also ask

How do I send a Curl request in terminal?

To make a GET request using Curl, run the curl command followed by the target URL. Curl automatically selects the HTTP GET request method unless you use the -X, --request, or -d command-line option.

How do I POST JSON with Curl?

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.

Does Curl use POST?

Users can send data with the POST request using the -d flag. The following POST request sends a user and a pass field along with their corresponding values. POSTing with curl's -d option will include a default header that looks like: Content-Type: application/x-www-form-urlencoded .


1 Answers

First off, there is an extraneous " at the end of your command.

Try this

curl -v \   -H "Accept: application/json" \   -H "Content-type: application/json" \   -X POST \   -d ' {"user":{"first_name":"firstname","last_name":"lastname","email":"[email protected]","password":"app123","password_confirmation":"app123"}}' \   http://localhost:3000/api/1/users 
like image 139
Bob Avatar answered Oct 13 '22 00:10

Bob