I've created a simple API with Sinatra that sends an email based on submitted JSON data. I can create a form, submit the JSON data via the form, and access params to get the to, subject, and body of the email. However, I'm trying to use cURL to test the API and can't seem to get things to work. I assume my formatting in the cURL request is busted. Below is a cURL request I've tried as well as the output of params as well as an attempt to parse params with the JSON gem.
I tend to get params with a giant key that is a string of my JSON data with a value of nil. I've tried adding Content-Type: application/json, and when I do this, params is empty.
curl -X POST -H "Accept: application/json" -d '{ "to": "Brantley <[email protected]>", "subject": "hello world", "body": "Hi Jennifer! Sending you an email via this API I just made." }' http://localhost:9393/send-email
Here is the params hash that is returned...
{"{ \"to\": \"Brantley <[email protected]>\", \"subject\": \"hello world\", \"body\": \"Hi Jennifer! Sending you an email via this API I just made.\" }"=>nil}
I try converting this to something more useful with JSON params, and then I get the following...
{\"{ \\\"to\\\": \\\"Brantley <[email protected]>\\\", \\\"subject\\\": \\\"hello world\\\", \\\"body\\\": \\\"Hi Jennifer! Sending you an email via this API I just made.\\\" }\":null}"
I've spent a solid day on this, have read 20 stackoverflow postings about similar issues, and am still stumped, so any advice would be helpful. Cheers!
I got it...no need to fool around with params. I can get the JSON payload as a hash by doing the following...
payload = JSON.parse(request.body.read)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With