通过Net :: HTTP在Ruby中发送http post请求 [英] Sending http post request in Ruby by Net::HTTP

查看:286
本文介绍了通过Net :: HTTP在Ruby中发送http post请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向网络服务发送带有自定义标头的请求。

I'm sending a request with custom headers to a web service.

require 'uri'
require 'net/http'

uri = URI("https://api.site.com/api.dll")
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
headers = 
{
    'HEADER1' => "VALUE1",
    'HEADER2' => "HEADER2"
}

response = https.post(uri.path, headers) 
puts response

它没有用,我收到的错误是:

It's not working, I'm receiving an error of:

/usr/lib/ruby/1.9.1/net/http.rb:1932:in `send_request_with_body': undefined method `bytesize' for #<Hash:0x00000001b93a10> (NoMethodError)

我该如何解决这个问题?

How do I solve this?

PS Ruby 1.9.3

推荐答案

<$ c $的第二个参数c> Net :: HTTP#post 需要是一个 String ,其中包含要发布的数据(通常是表单数据),标题将在可选的第三个参数。

The second argument of Net::HTTP#post needs to be a String containing the data to post (often form data), the headers would be in the optional third argument.

这篇关于通过Net :: HTTP在Ruby中发送http post请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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