Forum: Ruby sending a payload with JSON and REST

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
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!
This topic is locked and can not be replied to.