Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Build a Rails API Coding the API Rails API CSRF Verification

Question about POST Request with cURL to rails API

Hi there!

I am getting a bit confused over what to pass in with the -d attribute in the cURL request. In this example (the video), only the data for the field needs to be passed in:

-d '{"title":"The title goes here"}'

However, to get my own Rails API to work, I have to pass in the following in the data attribute, for example for Users model:

-d '{"user": {"firstname":"Matthew"}}'

If I don't do this, I get the missing parameter error for user. My question is what could be causing this issue?

Thanks in advance for any help!

Matt

Chris Vukin
Chris Vukin
17,787 Points

Snap man, was having this same problem thanks for posting this info! How will this affect our requests from other services? Will this extra json markup be needed as well? One issue, when I check the db it shows a created item with no params saved after running with the cURL request showing nested params, all are null. Any thoughts?

Here are some logs:

Mac-542696def70f:evr_splash_api medright1$ curl -i -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"patient": {"dob":"12.14.84"}}' http://localhost:3000/patients.json HTTP/1.1 200 OK X-Frame-Options: SAMEORIGIN X-Xss-Protection: 1; mode=block X-Content-Type-Options: nosniff Content-Type: application/json Etag: W/"7215ee9c7d9dc229d2921a40e899ec5f" Cache-Control: max-age=0, private, must-revalidate X-Request-Id: 4c0e03c5-b2e7-42ab-8670-3b08de7f4b63 X-Runtime: 1.268337 Server: WEBrick/1.3.1 (Ruby/2.2.0/2014-12-25) Date: Fri, 20 Mar 2015 19:35:50 GMT Content-Length: 1 Connection: Keep-Alive

and: /// <main>'2.2.0 :002 > Patient.all Patient Load (2.6ms) SELECT "patients".* FROM "patients" ActiveRecord::Relation [Patient id: 1, first_name: "John", last_name: "Doe", dob: nil, allergies: "codeine", code: "full", notes: nil, orders: nil, cbcNa: "138", cbcCl: nil, cbcBUN: nil, cbcK: nil, cmpHco3: nil, cmpCr: nil, cbcWbc: nil, hbg: nil, htc: nil, plt: nil, glucose: nil, bpSys: nil, bpDia: nil, o2: nil, pulse: nil, resp: nil, temp: nil, created_at: "2015-03-20 15:45:03", updated_at: "2015-03-20 15:45:03", Patient id: 2, first_name: nil, last_name: nil, dob: nil, allergies: nil, code: nil, notes: nil, orders: nil, cbcNa: nil, cbcCl: nil, cbcBUN: nil, cbcK: nil, cmpHco3: nil, cmpCr: nil, cbcWbc: nil, hbg: nil, htc: nil, plt: nil, glucose: nil, bpSys: nil, bpDia: nil, o2: nil, pulse: nil, resp: nil, temp: nil, created_at: "2015-03-20 19:35:50", updated_at: "2015-03-20 19:35:50" 2.2.0 :003 ///

1 Answer

Brandon Barrette
Brandon Barrette
20,485 Points

What is the route you are passing this to, meaning in your routes.rb file, what is the line for the POST route?

Also, for the controller method for this route, paste that too (just the method, not the whole controller)