How to Send an Http Put Request in Ruby

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')

Ruby: HTTP Put method

A few things:

  • You're not sending JSON in the Ruby example, it's a string representation of a Ruby hash which isn't the same. You need the JSON module or similar.
  • In the Ruby code you're attempting to send a JSON object (which would look like {"ip":"1.1.1.1"} and in the curl example you're sending it in application/x-www-form-urlencoded format, so they're currently not equivalent.
  • Also I'd look at the type of data the server expects from your requests: both Ruby and curl send a request header of Content-Type: application/x-www-form-urlencoded by default, and you're expecting to send JSON. This is why the curl example works: the data format you're using and the header matches. Note the .json in the URL shouldn't really make any difference; the header takes precedence.
  • Your call to send_request has you picking out the data parameter as a Python-style keyword argument. Ruby doesn't do that: what you're actually doing there is assigning a local variable in-line with the call.

So try something like this:

require 'json' # put this at the top of the file

uri = URI.parse("http://#{ip}:#{port}/api/v1/address_data/1.json")
jobj = {"ip" => "1.1.1.1"}
http = Net::HTTP.new(uri.hostname, uri.port)
response = http.send_request('PUT', uri.path, JSON.dump(jobj),
{'Content-Type' => 'application/json'})

And just a friendly reminder, saying something "doesn't work" doesn't usually give enough information to people that might answer your question: try and remember to paste in error messages, stack traces, and things like that :)

convert curl PUT to ruby net:http

Converted via Postman.

require 'uri'
require 'net/http'

url = URI("https://some.domain/repository/npm/-/user/org.couchdb.user:anonymous")

http = Net::HTTP.new(url.host, url.port)

request = Net::HTTP::Put.new(url)
request["Accept"] = 'application/json'
request["Content-Type"] = 'application/json'
request["Cache-Control"] = 'no-cache'
request["Postman-Token"] = '9e3622cd-fda5-458d-2521-325849546ef7'
request.body = "{\"name\": \"anonymous\", \"password\": \"obfuscated\"}"

response = http.request(request)
puts response.read_body

Uploading data via HTTP PUT without multi-part gutter

With net/http, set the request body to the file content as stream:

request.body_stream = File.open("#{file}")
request.content_length = File.size("#{file}")
request.content_type = "text/plain" # ?!

Determining the proper MIME type from the file alone is an adventure in itself.



Related Topics



Leave a reply



Submit