Sending JSON via cURL POST Sinatra request

I created a simple API with Sinatra that sends an email based on the submitted JSON data. I can create a form, send JSON data through the form and access the parameters to get the subject, subject and body of the message. However, I am trying to use cURL to test the API and cannot seem to make it work. I assume that my formatting in the cURL request is ruined. Below is the cURL request I tried, as well as the output of the parameters, as well as an attempt to parse the parameters using a JSON pearl.

I am trying to get parameters with a giant key, which is a string of my JSON data with a value of nil. I tried adding Content-Type: application / json, and when I do this, the options are empty.

curl -X POST -H "Accept: application/json" -d '{ "to": "Brantley < test@gmail.com >", "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 returns ...

 {"{ \"to\": \"Brantley < test@gmail.com >\", \"subject\": \"hello world\", \"body\": \"Hi Jennifer! Sending you an email via this API I just made.\" }"=>nil} 

I try to convert this to something more useful with JSON options, and then I get the following ...

 {\"{ \\\"to\\\": \\\"Brantley < test@gmail.com >\\\", \\\"subject\\\": \\\"hello world\\\", \\\"body\\\": \\\"Hi Jennifer! Sending you an email via this API I just made.\\\" }\":null}" 

I spent all day on this, read 20 stackoverflow posts about similar issues, and I'm still at a dead end, so any advice would be helpful. Hooray!

+6
source share
1 answer

I get it ... no need to cheat with parameters. I can get the JSON payload as a hash by doing the following ...

 payload = JSON.parse(request.body.read) 
+10
source

Source: https://habr.com/ru/post/970489/


All Articles