7

I'm trying to mimic a curl request using the RestClient Ruby gem, and so far, I've been having a lot of trouble trying to send in a payload. My curl request looks something like this

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

I've been trying to replicate this with RestClient using something like this:

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

Alas, I keep getting 400 - Bad Requests errors when doing this. Am I sending data over the wrong way? Should I be using something other than payload?

K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
rboling
  • 717
  • 1
  • 4
  • 8

3 Answers3

9

Change:

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

To:

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

Also, specify the headers.

So, it becomes:

RestClient::Request.execute(method: :post,
                            url: 'your_url',
                            user: 'API_KEY',
                            payload: '{"param_1": "1"}',
                            headers: {"Content-Type" => "application/json"}
                           )
K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
  • Everything is working fine with cURL. I'm curious how I'd make the equivalent query with RestClient – rboling Oct 25 '15 at 01:31
  • Thanks for the help! Ultimately, I had to pass in an argument to clarify that my data was being sent over in JSON. – rboling Oct 25 '15 at 01:59
  • Yes, you had another issue that your `payload` was not a string, it was a hash. My answer addressed that issue. Also, if it did not work, then I would tell you to add the `headers` option to the request. But you already did that, which is great :) – K M Rakibul Islam Oct 25 '15 at 02:01
  • I think you have to switch payload: '{"param_1" => "1"}' to payload: '{"param_1" : "1"}'. If you change that, I'll accept your answer! – rboling Oct 25 '15 at 02:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93275/discussion-between-k-m-rakibul-islam-and-rboling). – K M Rakibul Islam Oct 25 '15 at 02:37
  • Although those two are the same, using just different hash syntax and both should work, but I changed to `payload: '{"param_1" : "1"}'` which is the latest ruby hash syntax. – K M Rakibul Islam Oct 25 '15 at 02:39
4

Just Change:

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

To:

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

So, then it becomes:

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

Turns out I had to add an argument to specify that my data was in a 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"})

rboling
  • 717
  • 1
  • 4
  • 8