1

I sending data to a server using the Rho::AsyncHttp.post method, but i'm unable to send the data as json object. But sending the data as string reaches the server but the server api requires json data.

Here the code i'm using,

data = { :query1 => "this demo" , :query2 => "another demo" }
result = Rho::AsyncHttp.post(
    :url => "https://www.myserverapi.com/myapi",
    :body => data
)

The above code fails to connect to server. How ever when i change the :body => #{data}, its reaches the server but as string. Ant suggestion?

Ashis Kumar
  • 6,494
  • 2
  • 21
  • 36

1 Answers1

1

First, when you set the :body => #{data}, you are converting the json data to string doing #{}

Second, you need to set the :header to { "Content-Type" => "application/json; charset=utf-8", "Accept" => "application/json" }. This makes the post method accept the data as json object.

So you need the below code,

data = { :query1 => "this demo" , :query2 => "another demo" } # Any data to be passed
result = Rho::AsyncHttp.post(
    :url => "https://www.myserverapi.com/myapi",
    :header => { "Content-Type" => "application/json; charset=utf-8", "Accept" => "application/json" },
    :body => Rho::JSON.parse(data)
)

Hope this helps you.

Ashis Kumar
  • 6,494
  • 2
  • 21
  • 36