带标题的Ruby https帖子
如何在Ruby中使用json制作带有标题的Https帖子 我试过:带标题的Ruby https帖子,ruby,post,https,header,Ruby,Post,Https,Header,如何在Ruby中使用json制作带有标题的Https帖子 我试过: uri = URI.parse("https://...") https = Net::HTTP.new(uri.host,uri.port) req = Net::HTTP::Post.new(uri.path) req['foo'] = bar res = https.request(req) puts res.body 尝试: 问题是它是一个json。这解决了我的问题。不管怎样,我的问题不
uri = URI.parse("https://...")
https = Net::HTTP.new(uri.host,uri.port)
req = Net::HTTP::Post.new(uri.path)
req['foo'] = bar
res = https.request(req)
puts res.body
尝试:
问题是它是一个json。这解决了我的问题。不管怎样,我的问题不清楚,所以悬赏给了朱丽
require 'uri'
require 'net/http'
require 'net/https'
require 'json'
@toSend = {
"date" => "2012-07-02",
"aaaa" => "bbbbb",
"cccc" => "dddd"
}.to_json
uri = URI.parse("https:/...")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json'})
req['foo'] = 'bar'
req.body = "[ #{@toSend} ]"
res = https.request(req)
puts "Response #{res.code} #{res.message}: #{res.body}"
默认情况下的安全示例:
require 'net/http'
require 'net/https'
req = Net::HTTP::Post.new("/some/page.json", {'Content-Type' =>'application/json'})
req.body = your_post_body_json_or_whatever
http = Net::HTTP.new('www.example.com', 443)
http.use_ssl = true
http.ssl_version = :TLSv1 # ruby >= 2.0 supports :TLSv1_1 and :TLSv1_2.
# SSLv3 is broken at time of writing (POODLE), and it's old anyway.
http.verify_mode = OpenSSL::SSL::VERIFY_PEER # please don't use verify_none.
# if you want to verify a server is from a certain signing authority,
# (self-signed certs, for example), do this:
http.ca_file = 'my-signing-authority.crt'
response = http.start {|http| http.request(req) }
这里有一种更干净的方法来使用Net::HTTP。如果您只是想得到响应并扔掉其他对象,这是非常有用的
require 'net/http'
require 'json'
uri = URI("https://example.com/path")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Post.new(uri)
req['Content-Type'] = 'application/json'
# The body needs to be a JSON string, use whatever you know to parse Hash to JSON
req.body = {a: 1}.to_json
http.request(req)
end
# The "res" is what you need, get content from "res.body". It's a JSON string too.
其工作原理是,您可以按如下方式传递数据和标题:
header = {header part}
data = {"a"=> "123"}
uri = URI.parse("https://anyurl.com")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
req = Net::HTTP::Post.new(uri.path, header)
req.body = data.to_json
res = https.request(req)
puts "Response #{res.code} #{res.message}: #{res.body}"
这个密码不知怎么的对我不起作用。我必须执行req.set_form_data(@toSend)来正确发送数据,而不是@toSend={}.to_json。希望这能帮助其他陷入困境的人。我不需要使用HTTPS,但在这里找到了一个使用自定义标题的工作解决方案:应该是
initheader:{'Content-Type'=>'application/json'}
如何设置正文和标题?req.body=“the body”
https.use\u ssl=true是连接到https服务器所需的内容。如何设置正文和标题?{'Content-Type'=>'application/json'}是成为标题的哈希。“你的身体”就是你的身体。美丽!不知道您可以传入使用\u ssl
作为属性!非常感谢。
header = {header part}
data = {"a"=> "123"}
uri = URI.parse("https://anyurl.com")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
req = Net::HTTP::Post.new(uri.path, header)
req.body = data.to_json
res = https.request(req)
puts "Response #{res.code} #{res.message}: #{res.body}"