@@ -67,6 +67,8 @@ class HTTPHeaderSyntaxError < StandardError; end
67
67
# Net::HTTP.post(uri, data)
68
68
# params = {title: 'foo', body: 'bar', userId: 1}
69
69
# Net::HTTP.post_form(uri, params)
70
+ # data = '{"title": "foo", "body": "bar", "userId": 1}'
71
+ # Net::HTTP.put(uri, data)
70
72
#
71
73
# - If performance is important, consider using sessions, which lower request overhead.
72
74
# This {session}[rdoc-ref:Net::HTTP@Sessions] has multiple requests for
@@ -524,6 +526,8 @@ class HTTPHeaderSyntaxError < StandardError; end
524
526
# Sends a POST request with form data and returns a response object.
525
527
# - {::post}[rdoc-ref:Net::HTTP.post]:
526
528
# 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.
527
531
# - {#copy}[rdoc-ref:Net::HTTP#copy]:
528
532
# Sends a COPY request and returns a response object.
529
533
# - {#delete}[rdoc-ref:Net::HTTP#delete]:
@@ -893,6 +897,39 @@ def HTTP.post_form(url, params)
893
897
}
894
898
end
895
899
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
+
896
933
#
897
934
# \HTTP session management
898
935
#
@@ -2016,6 +2053,11 @@ def patch(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segm
2016
2053
# http = Net::HTTP.new(hostname)
2017
2054
# http.put('/todos/1', data) # => #<Net::HTTPOK 200 OK readbody=true>
2018
2055
#
2056
+ # Related:
2057
+ #
2058
+ # - Net::HTTP::Put: request class for \HTTP method PUT.
2059
+ # - Net::HTTP.put: sends PUT request, returns response body.
2060
+ #
2019
2061
def put ( path , data , initheader = nil )
2020
2062
request ( Put . new ( path , initheader ) , data )
2021
2063
end
0 commit comments