1

Basically, I want to send post request to a faye server. When I use curl, It works,

curl http://localhost:9292/faye -d 'message={"channel":"/messages/new", "data":"hello"}'

I can see hello in client.

but when I use http_party or the rest_client or net http, It says bad request.

def send

     params = {'channel' => 'faye/messages/new','data' => 'hello', "Content-type" => "application/json"}
     x = Net::HTTP::Post.new (URI.parse('http://localhost:9292/faye'))
     puts x.body

    # I tried it with http_ party too and rest_cliet.
    #  data = {
    #    channel: "messages/new",
    #    data: "hello"
    #  }
    #  RestClient.post 'http://localhost:9292/faye', :channel => 'messages/new', :data=> "hello"
    # response = RestClient::Request.execute(method: :post, url: '127.0.0.1:9292/faye',messages: {"channel" => "messages/new", "data"=>"hello"})
   end
end

I have tried slolutions from Submitting POST data from the controller in rails to another website

and Ruby on Rails HTTPS Post Bad Request but it still says bad request.

I dont know where I am making mistake. I have been pulling my hair in this for some days. My ruby version is 2.2.4 rails -v => 4.2.5

Edit: with help from accepted answer, I update my method. Its working now.

require 'rest_client'
   require 'json'
   def send
     RestClient.post 'http://localhost:9292/faye', {message: {"channel" => "/messages/new", "data"=>"hello"}.to_json}, {:content_type => :json, :accept => :json}
   end
Community
  • 1
  • 1
Ccr
  • 686
  • 7
  • 25

1 Answers1

3

In your curl example you're sending json data, but not when using Ruby. Try this

require "json"
RestClient.post("http://localhost:9292/faye", {
  message: {"channel" => "messages/new", "data"=>"hello"}.to_json
})
Linus Oleander
  • 17,746
  • 15
  • 69
  • 102
  • 1
    many many thanks man, It worked, also I found another mistake. the channel path should be "channel" => "/messages.new". now it works. :) – Ccr Feb 09 '16 at 04:45