20

我正在尝试将 PUT 请求发送到特定的 URL,但到目前为止还没有成功。

如果我是通过一个 HTTP 请求者 GUI 来做的,比如这个,它就像在以下 url 上做一个 PUT 一样简单:

http://www.mywebsite.com:port/Application?key=apikey&id=id&option=enable|disable

请注意,在上述请求中指定了端口号。通过 ruby​​ 代码提交请求时,我也需要这样做。

如何在 Ruby 中复制这样的请求?

4

1 回答 1

34
require 'net/http'

port = 8080
host = "127.0.0.1"
path = "/Application?key=apikey&id=id&option=enable"

req = Net::HTTP::Put.new(path, initheader = { 'Content-Type' => 'text/plain'})
req.body = "whatever"
response = Net::HTTP.new(host, port).start {|http| http.request(req) }
puts response.code

Larry's answer helped point me in the right direction. A little more digging helped me find a more elegant solution guided by this answer.

http = Net::HTTP.new('www.mywebsite.com', port)
response = http.send_request('PUT', '/path/from/host?id=id&option=enable|disable')
于 2012-07-09T21:56:03.823 回答