Skip to content

Commit ce895f6

Browse files
authored
Merge pull request #175 from Zopolis4/betty
Add Net::HTTP.put method
2 parents ab525c9 + 6dc01c9 commit ce895f6

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

lib/net/http.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class HTTPHeaderSyntaxError < StandardError; end
6767
# Net::HTTP.post(uri, data)
6868
# params = {title: 'foo', body: 'bar', userId: 1}
6969
# Net::HTTP.post_form(uri, params)
70+
# data = '{"title": "foo", "body": "bar", "userId": 1}'
71+
# Net::HTTP.put(uri, data)
7072
#
7173
# - If performance is important, consider using sessions, which lower request overhead.
7274
# This {session}[rdoc-ref:Net::HTTP@Sessions] has multiple requests for
@@ -524,6 +526,8 @@ class HTTPHeaderSyntaxError < StandardError; end
524526
# Sends a POST request with form data and returns a response object.
525527
# - {::post}[rdoc-ref:Net::HTTP.post]:
526528
# Sends a POST request with data and returns a response object.
529+
# - {::put}[rdoc-ref:Net::HTTP.put]:
530+
# Sends a PUT request with data and returns a response object.
527531
# - {#copy}[rdoc-ref:Net::HTTP#copy]:
528532
# Sends a COPY request and returns a response object.
529533
# - {#delete}[rdoc-ref:Net::HTTP#delete]:
@@ -893,6 +897,39 @@ def HTTP.post_form(url, params)
893897
}
894898
end
895899

900+
# Sends a PUT request to the server; returns a Net::HTTPResponse object.
901+
#
902+
# Argument +url+ must be a URL;
903+
# argument +data+ must be a string:
904+
#
905+
# _uri = uri.dup
906+
# _uri.path = '/posts'
907+
# data = '{"title": "foo", "body": "bar", "userId": 1}'
908+
# headers = {'content-type': 'application/json'}
909+
# res = Net::HTTP.put(_uri, data, headers) # => #<Net::HTTPCreated 201 Created readbody=true>
910+
# puts res.body
911+
#
912+
# Output:
913+
#
914+
# {
915+
# "title": "foo",
916+
# "body": "bar",
917+
# "userId": 1,
918+
# "id": 101
919+
# }
920+
#
921+
# Related:
922+
#
923+
# - Net::HTTP::Put: request class for \HTTP method +PUT+.
924+
# - Net::HTTP#put: convenience method for \HTTP method +PUT+.
925+
#
926+
def HTTP.put(url, data, header = nil)
927+
start(url.hostname, url.port,
928+
:use_ssl => url.scheme == 'https' ) {|http|
929+
http.put(url, data, header)
930+
}
931+
end
932+
896933
#
897934
# \HTTP session management
898935
#
@@ -2016,6 +2053,11 @@ def patch(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segm
20162053
# http = Net::HTTP.new(hostname)
20172054
# http.put('/todos/1', data) # => #<Net::HTTPOK 200 OK readbody=true>
20182055
#
2056+
# Related:
2057+
#
2058+
# - Net::HTTP::Put: request class for \HTTP method PUT.
2059+
# - Net::HTTP.put: sends PUT request, returns response body.
2060+
#
20192061
def put(path, data, initheader = nil)
20202062
request(Put.new(path, initheader), data)
20212063
end

lib/net/http/requests.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ class Net::HTTP::Post < Net::HTTPRequest
124124
# - {Idempotent}[https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Idempotent_methods]: yes.
125125
# - {Cacheable}[https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Cacheable_methods]: no.
126126
#
127+
# Related:
128+
#
129+
# - Net::HTTP.put: sends +PUT+ request, returns response object.
130+
# - Net::HTTP#put: sends +PUT+ request, returns response object.
131+
#
127132
class Net::HTTP::Put < Net::HTTPRequest
128133
METHOD = 'PUT'
129134
REQUEST_HAS_BODY = true

0 commit comments

Comments
 (0)