How to send data in a mail request using RestClient

I am trying to reproduce a curl request using the Restclient Ruby stone, and so far I have had a lot of problems trying to send the payload. My curl request looks something like this.

curl URL -X POST -u API_KEY -d '{"param_1": "1"}'

I am trying to replicate this using RestClient using something like this:

RestClient::Request.execute(method: :post, url: URL, user: API_KEY, payload: {"param_1" => "1"})

Alas, I keep getting 400 errors - Bad Requests at the same time. Am I sending data differently? Should I use something other than the payload?

+6
source share
3 answers

Edit:

 payload: {"param_1" => "1"}) 

To:

 payload: '{"param_1": "1"})' 

Also specify headers .

So he becomes:

 RestClient::Request.execute(method: :post, url: 'your_url', user: 'API_KEY', payload: '{"param_1": "1"}', headers: {"Content-Type" => "application/json"} ) 
+6
source

Just change:

 payload: {"param_1" => "1"} 

To:

 payload: {"param_1" => "1"}.to_json 

So then it will be:

 RestClient::Request.execute(method: :post, url: 'your_url', user: 'API_KEY', payload: {"param_1" => "1"}.to_json, headers: {"Content-Type" => "application/json"} ) 
+2
source

Turns out I had to add an argument to indicate that my data was in JSON format. The correct answer was something like this: RestClient::Request.execute(method: :post, url: URL, user: API_KEY, payload: '{"param_1": "1"}', headers: {"Content-Type" => "application/json"})

+1
source

All Articles