Forum: Ruby sending a payload with JSON and REST

Ee6fb5cad9d1b269f1dcbb5a76c9d760?d=identicon&s=25 Moutalib Abdel (jarh)
on 2014-08-10 13:17
Hello all,

I'm new to the JSON and REST modules, and i'm struggling to send the
following payload information as a post method:

url: 'http://site.local/trim/v1/config/login'
method: post
request header: Content-Type:xxxx/xxx+json
payload:
{
 "login":
      {
        "username":"admin",
        "password":"secret"
     }
}
===============================================
code:
require 'JSON'
require 'rest-client'

post = RestClient.post(url, {:params => {login => 'username','admin',
'password','secret'}}, {"Content-Type" => "application/tribapte" })
puts post.args[:payload][:params]
===============================================

The problem is that the ':params' are not matching the ruby hash
structure, so this code will never run successful.

Hopefully someone can guide me trough the process
of successfully post the payload as mentioned above.

Thanks in advance!

iLias
4a65f01f7ece0b720bdb0de3c3db089e?d=identicon&s=25 Dansei Yuuki (blutorange)
on 2014-08-10 18:15
https://github.com/rest-client/rest-client/blob/ma...
> def self.post(url, payload, headers={}, &block)

Does this work?

> url = 'http://foo.bar/foobar'
> payload = { 'login' => {'username' => 'admin', 'password' => 'secret'}}
> initheader = { 'Content-Type' => 'xxxx/xxx+json' }
> RestClient.post(url, payload, initheader)

Generating the json payload yourself should work as well

> encoded = JSON.generate(payload)
> RestClient.post(url, encoded, initheader)
Ee6fb5cad9d1b269f1dcbb5a76c9d760?d=identicon&s=25 Moutalib Abdel (jarh)
on 2014-08-10 21:44
Thanks Dansei that was indeed very helpful!

Cheers!
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.