Sending a payload with JSON and REST

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

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)

Thanks Dansei that was indeed very helpful!

Cheers!