Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP post request via Ruby

I'm very new to ruby and trying some basic stuff. When I send HTTP request to the server using:

curl -v -H "Content-Type: application/json" -X GET -d "{"myrequest":"myTest","reqid":"44","data":{"name":"test"}}" localhost:8099

My server sees JSON data as "{myrequest:myTest,reqid:44,data:{name:test}}"

But when I send the request using the following ruby code:

require 'net/http'

@host = 'localhost'
@port = '8099'

@path = "/posts"

@body = ActiveSupport::JSON.encode({
    :bbrequest => "BBTest", 
    :reqid => "44", 
    :data => 
    {
        :name => "test"
    }
})

request = Net::HTTP::Post.new(@path, initheader = {'Content-Type' =>'application/json'})
request.body = @body
response = Net::HTTP.new(@host, @port).start {|http| http.request(request) }
puts "Response #{response.code} #{response.message}: #{response.body}"

It sees it as "{\"bbrequest\":\"BBTest\",\"reqid\":\"44\",\"data\":{\"name\":\" test\"}}" and server is unable to parse it. Perhaps there are some extra options I need to set to send request from Ruby to exclude those extra characters?

Can you please help. Thanks in advance.

like image 973
Bilal Wahla Avatar asked Jul 05 '11 13:07

Bilal Wahla


People also ask

How do you create a post request in Ruby?

POST request with do block Using the object created in the do block we use the . body object method to add our params encoded using URI. There are other useful methods like request. headers['Content-Type'] = 'application/json' here setting the request's content type, or request.

What is Net HTTP?

net. http is an API for making non-blocking HTTP requests to remote servers.


1 Answers

What you are doing on the shell produces invalid JSON. Your server should not accept it.

  $echo "{"myrequest":"myTest","reqid":"44","data":{"name":"test"}}"
      {myrequest:myTest,reqid:44,data:{name:test}}

This is JSON with unescaped keys and values, will NEVER work. http://jsonlint.com/ If your server accept this "sort of kind of JSON" but does not accept the second one in your example your server is broken.

My server sees JSON data as "{myrequest:myTest,reqid:44,data:{name:test}}"

Your server sees a string. When you will try to parse it into JSON it will produce an error or garbage.

It sees it as "{\"bbrequest\":\"BBTest\",\"reqid\":\"44\",\"data\":{\"name\":\" test\"}}"

No this is how it's printed via Ruby's Object#inspect. You are printing the return value of inspect somewhere and then trying to judge whether it's valid JSON - it is not, since this string you've pasted in is made to be pasted into the interactive ruby console (irb) or into a ruby script, and it contains builtin escapes. You need to see your JSON string raw, just print the string instead of inspecting it.

I think your server is either broken or not finished yet, your curl example is broken and your ruby script is correct and will work once the server is fixed (or finished). Simply because

  irb(main):002:0> JSON.parse("{\"bbrequest\":\"BBTest\",\"reqid\":\"44\",\"data\":{\"name\":\" test\"}}")
        # => {"bbrequest"=>"BBTest", "reqid"=>"44", "data"=>{"name"=>" test"}}
like image 69
Julik Avatar answered Oct 03 '22 06:10

Julik