How can I send an HTTP PUT request in 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
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')
finiteloop
Front-end developer with a passion for building quality user interfaces. Always keeping a mind on developer ergonomics, and automation to work toward and maintain higher code quality.
Updated on July 10, 2020Comments
-
finiteloop almost 4 years
I am trying to send a PUT request to a particular URL, and have thus far been unsuccessful in doing so.
If I were doing it through an HTTP requester GUI, such as this one, it would be as simple as doing a PUT on the following url:
http://www.mywebsite.com:port/Application?key=apikey&id=id&option=enable|disable
Note that a port number is specified in the above request. I will also need to do that when submitting the request through the ruby code.
How can I replicate such a request in Ruby?
-
finiteloop almost 12 yearsWhere is this would I include the arguments for my request?
-
finiteloop almost 12 yearsLarry, this is very helpful. Thanks for taking the time to do that.
-
rampion about 11 years@finiteloop:
send_request
takes a third parameter for the data you're 'PUT'ing -
jaybrau almost 6 yearssend_request also takes an optional fourth parameter for custom headers.