带有标头的 Ruby https POST [英] Ruby https POST with headers

查看:20
本文介绍了带有标头的 Ruby https POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Ruby 中使用 json 制作带有标头的 Https 帖子?

How can I make a Https post with a header in Ruby with a 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.这解决了我的问题.无论如何,我的问题还不清楚,所以赏金给了 Juri

The problem it was a json. This solve my problem. Anyway, my question was not clear, so the bounty goes to Juri

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}"

这篇关于带有标头的 Ruby https POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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