26

I need to send data in JSON to another app which runs on the same computer.
I send request like so (rails 3.2.13 )

 data = { //some data hash }
 url = URI.parse('http://localhost:6379/api/plans')
  resp, data = Net::HTTP.post_form(url, data.to_JSON )
  p resp
  p data
  { resp: resp, data: data.to_JSON }

But i get Net::HTTPBadResponse (wrong status line: "-ERR unknown command 'POST'"): How can i solve this problem?

Update 1
Updated my code as @Raja-d suggested

  url = URI.parse('http://localhost:6379/v1/sessions')
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  resp, data = Net::HTTP.post_form(url, data)
  p resp
  p data

But i still get error Net::HTTPBadResponse (wrong status line: "-ERR unknown command 'POST'"):

Elmor
  • 4,775
  • 6
  • 38
  • 70

1 Answers1

55

I don't know what your problem is but what about something like this

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

request = Net::HTTP::Post.new(uri.path, {'Content-Type' => 'application/json'})
request.body = data.to_json

response = http.request(request)
oldergod
  • 15,033
  • 7
  • 62
  • 88
  • 4
    why not `http.post(..` call directly ? :) this [method](http://docs.ruby-lang.org/en/2.0.0/Net/HTTP.html#method-i-post) – Arup Rakshit Jul 05 '15 at 12:12
  • 1
    How to add post parameters to http.post? – Nathan B Oct 18 '21 at 07:39
  • 1
    @NathanB in know this is coming 3+ years too late, but just in case others have the same question, the params are passed into the data variable. data = {"whatever": array_or_hash, "password_param": "password"} then on the target site, you can call params["whatever"] or params["password_param"] to access the data sent. – tomb Dec 15 '21 at 23:06