使用net/http发送发帖请求 [英] Sending a Post request with net/http

查看:112
本文介绍了使用net/http发送发帖请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将JSON中的数据发送到同一台计算机上运行的另一个应用程序.
我这样发送请求(rails 3.2.13)

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 }

但是我得到了Net::HTTPBadResponse (wrong status line: "-ERR unknown command 'POST'"): 我怎么解决这个问题?

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

更新1
将我的代码更新为@ Raja-d建议

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

但是我仍然收到错误Net::HTTPBadResponse (wrong status line: "-ERR unknown command 'POST'"):

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

推荐答案

我不知道您的问题是什么,但是类似这样的事情

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)

这篇关于使用net/http发送发帖请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆