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 trialGregg Mojica
11,506 PointsRails API cURL
Hey Guys! I'm fairly new to rails so pardon my ignorance here.
I have a very basic API setup (using the rails-api gem). I am however unsure how to send a POST request using cURL in terminal. Any thoughts based on the screenshot below?
http://s30.postimg.org/bwa3z8bz5/Screen_Shot_2015_10_20_at_7_55_09_PM.png
2 Answers
Tim Knight
28,888 PointsGregg, I had a pretty long answer typed out for you but accidentally closed the window. Basically you won't need to use .json in your POST request... Rails should detect it with just /tasks. If you want to post JSON you just need to send the JSON header with it:
curl -H "Content-Type: application/json" -X POST -d '{"task":{"name":"My Task"}}’ http://localhost:3000/tasks
This is assuming that you have a name in your task model. Also make sure that you have a params.require setup for whatever field(s) you're trying to send. You should see that within the task_params method at the bottom of the controller. Just name sure it includes the name
field or whatever else you're trying to submit.
Tim Knight
28,888 PointsGregg,
The top voted answer at http://superuser.com/questions/149329/what-is-the-curl-command-line-syntax-to-do-a-post-request has some syntax information on how to use curl for sending a POST request.
Gregg Mojica
11,506 PointsSaw that, still a bit unsure. I'm an iOS dev, so rails isn't my strength (yet)!! Any thoughts?
Tim Knight
28,888 PointsYes but what you're talking about here isn't a Rails specific issue, curl is a command line tool.
Let's say you have your local server running at http://localhost:8500. You'd post your data to http://localhost:8500/tasks. Try:
curl --data "param1=value1¶m2=value2" http://localhost:8500/tasks
Replacing param1 and param2 with items for your tasks object.
Another tool you might want to check out is HTTP Client it's something I use often.
Gregg Mojica
11,506 PointsI'm well aware of cURL is know it's not rails. I use http requests in my iOS apps all the time. I am unsure what the curl command to connect with this app is. Honestly not very confident with rails yet and as such'm trying to figure out how to do this. It's literally a barebones scaffold -- I just did rails g scaffold tasks name
The json is at localhost:3000/tasks.json
How do I send data to this? Just wondering what I need in the body. Thanks.
Gregg Mojica
11,506 PointsGregg Mojica
11,506 PointsTim, thanks so much for providing this. I really appreciate it. That's exactly what I needed. Thanks for putting up with me! FYI to anyone else who might see this thread that you need to add a " after My Task!
Tim Knight
28,888 PointsTim Knight
28,888 PointsNo problem Gregg, enjoy learning Rails... I know it can be weird sometimes. And I corrected my lack of ".
Gregg Mojica
11,506 PointsGregg Mojica
11,506 PointsThanks, Tim! Hope it'll prove to be a decent backend. I've bene using node (which I love) but wanted to switch things up a bit!