Skip to content

Commit

Permalink
Merge pull request #175 from Zopolis4/betty
Browse files Browse the repository at this point in the history
Add Net::HTTP.put method
  • Loading branch information
hsbt authored May 30, 2024
2 parents ab525c9 + 6dc01c9 commit ce895f6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
42 changes: 42 additions & 0 deletions lib/net/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class HTTPHeaderSyntaxError < StandardError; end
# Net::HTTP.post(uri, data)
# params = {title: 'foo', body: 'bar', userId: 1}
# Net::HTTP.post_form(uri, params)
# data = '{"title": "foo", "body": "bar", "userId": 1}'
# Net::HTTP.put(uri, data)
#
# - If performance is important, consider using sessions, which lower request overhead.
# This {session}[rdoc-ref:Net::HTTP@Sessions] has multiple requests for
Expand Down Expand Up @@ -524,6 +526,8 @@ class HTTPHeaderSyntaxError < StandardError; end
# Sends a POST request with form data and returns a response object.
# - {::post}[rdoc-ref:Net::HTTP.post]:
# Sends a POST request with data and returns a response object.
# - {::put}[rdoc-ref:Net::HTTP.put]:
# Sends a PUT request with data and returns a response object.
# - {#copy}[rdoc-ref:Net::HTTP#copy]:
# Sends a COPY request and returns a response object.
# - {#delete}[rdoc-ref:Net::HTTP#delete]:
Expand Down Expand Up @@ -893,6 +897,39 @@ def HTTP.post_form(url, params)
}
end

# Sends a PUT request to the server; returns a Net::HTTPResponse object.
#
# Argument +url+ must be a URL;
# argument +data+ must be a string:
#
# _uri = uri.dup
# _uri.path = '/posts'
# data = '{"title": "foo", "body": "bar", "userId": 1}'
# headers = {'content-type': 'application/json'}
# res = Net::HTTP.put(_uri, data, headers) # => #<Net::HTTPCreated 201 Created readbody=true>
# puts res.body
#
# Output:
#
# {
# "title": "foo",
# "body": "bar",
# "userId": 1,
# "id": 101
# }
#
# Related:
#
# - Net::HTTP::Put: request class for \HTTP method +PUT+.
# - Net::HTTP#put: convenience method for \HTTP method +PUT+.
#
def HTTP.put(url, data, header = nil)
start(url.hostname, url.port,
:use_ssl => url.scheme == 'https' ) {|http|
http.put(url, data, header)
}
end

#
# \HTTP session management
#
Expand Down Expand Up @@ -2016,6 +2053,11 @@ def patch(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segm
# http = Net::HTTP.new(hostname)
# http.put('/todos/1', data) # => #<Net::HTTPOK 200 OK readbody=true>
#
# Related:
#
# - Net::HTTP::Put: request class for \HTTP method PUT.
# - Net::HTTP.put: sends PUT request, returns response body.
#
def put(path, data, initheader = nil)
request(Put.new(path, initheader), data)
end
Expand Down
5 changes: 5 additions & 0 deletions lib/net/http/requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ class Net::HTTP::Post < Net::HTTPRequest
# - {Idempotent}[https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Idempotent_methods]: yes.
# - {Cacheable}[https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Cacheable_methods]: no.
#
# Related:
#
# - Net::HTTP.put: sends +PUT+ request, returns response object.
# - Net::HTTP#put: sends +PUT+ request, returns response object.
#
class Net::HTTP::Put < Net::HTTPRequest
METHOD = 'PUT'
REQUEST_HAS_BODY = true
Expand Down

0 comments on commit ce895f6

Please sign in to comment.