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 thedata
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
Ruby: Array Contained in Array, Any Order
Changing Songs on Jplayer by Clicking a Link, Hosted on Amazon S3
Obtaining a Facebook Auth Token for a Command-Line (Desktop) Application
Save All Image Files from a Website
Nokogiri: Searching for <Div> Using Xpath
Converting a Unique Seed String into a Random, Yet Deterministic, Float Value in Ruby
Generate Activerecord Schema from an Existing Table
Your Ruby Version Is 2.1.0, But Your Gemfile Specified 2.0.0
Override "Show" Resource Route in Rails
Interested in What the "<<" Does
Why Doesn't Relative_Require Work on Ruby 1.8.6
Ruby: Method Inexplicably Overwritten and Set to Nil
Inherit Class-Level Instance Variables in Ruby
Undefined Method (Nomethoderror) Ruby
Homebrew Install Ruby Keg-Only Can't Find Gem
Paperclip Error: Model Missing Required Attr_Accessor for 'Avatar_File_Name'
Using Nokogiri to Split Content on Br Tags
What Are the Ruby Win32API Parameters | How to Pass a Null Pointer