如何在Ruby中发送HTTP PUT请求?
我正在尝试向特定URL发送PUT请求,并且到目前为止还没有成功。
如果我是通过HTTP请求者GUI(例如这个)进行的 ,那么就像在以下url上执行PUT一样简单:
http://www.mywebsite.com:port/Application?key=apikey&id=id&option=enable|disable
请注意,在上述请求中指定了端口号。 通过ruby代码提交请求时,我还需要这样做。
我怎样才能在Ruby中复制这样的请求?
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
拉里的回答帮助我指明了正确的方向。 多一点挖掘帮助我找到了一个更优雅的解决方案,以此答案为指导。
http = Net::HTTP.new('www.mywebsite.com', port) response = http.send_request('PUT', '/path/from/host?id=id&option=enable|disable')