Sending a Post request with net/http

∥☆過路亽.° 提交于 2019-12-03 08:42:38

问题


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'"):


回答1:


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)


来源:https://stackoverflow.com/questions/17163507/sending-a-post-request-with-net-http

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!