make a http patch request in ruby

You can make an HTTP PATCH request in Ruby using the httparty gem. Here's an example:

main.rb
require 'httparty'

response = HTTParty.patch('https://example.com/api/resource',
                          headers: { 'Content-Type' => 'application/json' },
                          body: { key: 'value' }.to_json)

if response.success?
  puts 'Resource updated!'
else
  puts 'There was an error updating the resource.'
end
326 chars
12 lines

In this example:

  • We require the httparty gem.
  • We make a PATCH request to https://example.com/api/resource.
  • We include a Content-Type header of application/json.
  • We include a JSON payload in the request body.
  • We check the response for a successful status code (in the 2xx range).

gistlibby LogSnag