diff --git a/lib/net/http.rb b/lib/net/http.rb index a82de9d2..6c43e623 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -82,6 +82,7 @@ class HTTPHeaderSyntaxError < StandardError; end # body = 'Some text' # http.post(path, body) # Can also have a block. # http.put(path, body) + # http.query(path, body) # Can also have a block. # http.delete(path) # http.options(path) # http.trace(path) @@ -207,6 +208,7 @@ class HTTPHeaderSyntaxError < StandardError; end # - #options: OPTIONS. # - #trace: TRACE. # - #patch: PATCH. + # - #query, #request_query: QUERY. # # - {WebDAV methods}[https://en.wikipedia.org/wiki/WebDAV#Implementation]: # @@ -553,6 +555,8 @@ class HTTPHeaderSyntaxError < StandardError; end # Sends a PROPPATCH request and returns a response object. # - {#put}[rdoc-ref:Net::HTTP#put]: # Sends a PUT request and returns a response object. + # - {#query}[rdoc-ref:Net::HTTP#query]: + # Sends a QUERY request and returns a response object. # - {#request}[rdoc-ref:Net::HTTP#request]: # Sends a request and returns a response object. # - {#request_get}[rdoc-ref:Net::HTTP#request_get]: @@ -2101,6 +2105,25 @@ def patch(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segm send_entity(path, data, initheader, dest, Patch, &block) end + # Sends a QUERY request to the server; + # returns an instance of a subclass of Net::HTTPResponse. + # + # The request is based on the Net::HTTP::Query object + # created from string +path+, string +data+, and initial headers hash +initheader+. + # + # data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}' + # http = Net::HTTP.new(hostname) + # http.query('/todos/1', data) # => # + # + # Related: + # + # - Net::HTTP::Query: request class for \HTTP method QUERY. + # - Net::HTTP.query: sends QUERY request, returns response body. + # + def query(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+ + send_entity(path, data, initheader, dest, Query, &block) + end + # Sends a PUT request to the server; # returns an instance of a subclass of Net::HTTPResponse. # @@ -2335,6 +2358,36 @@ def request_put(path, data, initheader = nil, &block) #:nodoc: request Put.new(path, initheader), data, &block end + # Sends a QUERY request to the server; + # returns an instance of a subclass of Net::HTTPResponse. + # + # The request is based on the Net::HTTP::Query object + # created from string +path+, string +body+, and initial headers hash +initheader+. + # + # http = Net::HTTP.new(hostname) + # http.query('/todos/1', 'xyzzy') + # # => # + # + # With no block given, returns the response object: + # + # http = Net::HTTP.new(hostname) + # http.request_query('/todos') # => # + # + # With a block given, calls the block with the response object + # and returns the response object: + # + # http.request_query('/todos') do |res| + # p res + # end # => # + # + # Output: + # + # # + # + def request_query(path, data, initheader = nil, &block) # :yield: +response+ + request Query.new(path, initheader), data, &block + end + alias get2 request_get #:nodoc: obsolete alias head2 request_head #:nodoc: obsolete alias post2 request_post #:nodoc: obsolete @@ -2430,7 +2483,7 @@ def send_entity(path, data, initheader, dest, type, &block) # :stopdoc: - IDEMPOTENT_METHODS_ = %w/GET HEAD PUT DELETE OPTIONS TRACE/.freeze # :nodoc: + IDEMPOTENT_METHODS_ = %w/GET HEAD PUT DELETE OPTIONS TRACE QUERY/.freeze # :nodoc: def transport_request(req) count = 0 diff --git a/lib/net/http/request.rb b/lib/net/http/request.rb index 4a138572..db7641a7 100644 --- a/lib/net/http/request.rb +++ b/lib/net/http/request.rb @@ -61,6 +61,7 @@ # - Net::HTTP::Options # - Net::HTTP::Trace # - Net::HTTP::Patch +# - Net::HTTP::Query # # Subclasses for WebDAV requests: # diff --git a/lib/net/http/requests.rb b/lib/net/http/requests.rb index 8dc79a9f..2685b797 100644 --- a/lib/net/http/requests.rb +++ b/lib/net/http/requests.rb @@ -271,6 +271,41 @@ class Net::HTTP::Patch < Net::HTTPRequest RESPONSE_HAS_BODY = true end +# \Class for representing +# {HTTP method QUERY}[https://www.rfc-editor.org/rfc/rfc10008.html]: +# +# require 'net/http' +# uri = URI('http://example.com') +# hostname = uri.hostname # => "example.com" +# uri.path = '/posts' +# req = Net::HTTP::Query.new(uri) # => # +# req.body = '{"title": "foo","body": "bar","userId": 1}' +# req.content_type = 'application/json' +# res = Net::HTTP.start(hostname) do |http| +# http.request(req) +# end +# +# See {Request Headers}[rdoc-ref:Net::HTTPRequest@Request+Headers]. +# +# Properties: +# +# - Request body: yes. +# - Response body: yes. +# - {Safe}[https://en.wikipedia.org/wiki/HTTP#Safe_method]: yes. +# - {Idempotent}[https://en.wikipedia.org/wiki/HTTP#Idempotent_method]: yes. +# - {Cacheable}[https://en.wikipedia.org/wiki/HTTP#Cacheable_method]: yes. +# +# Related: +# +# - Net::HTTP#query: sends +QUERY+ request, returns response object. +# +class Net::HTTP::Query < Net::HTTPRequest + # :stopdoc: + METHOD = 'QUERY' + REQUEST_HAS_BODY = true + RESPONSE_HAS_BODY = true +end + # # WebDAV methods --- RFC2518 # diff --git a/sig/net-http.rbs b/sig/net-http.rbs index b54eb936..cc19054d 100644 --- a/sig/net-http.rbs +++ b/sig/net-http.rbs @@ -94,6 +94,7 @@ module Net # body = 'Some text' # http.post(path, body) # Can also have a block. # http.put(path, body) + # http.query(path, body) # Can also have a block. # http.delete(path) # http.options(path) # http.trace(path) @@ -222,6 +223,7 @@ module Net # * #options: OPTIONS. # * #trace: TRACE. # * #patch: PATCH. + # * #query, #request_query: QUERY. # # * [WebDAV methods](https://en.wikipedia.org/wiki/WebDAV#Implementation): # @@ -561,6 +563,8 @@ module Net # returns a response object. # * [#put](rdoc-ref:Net::HTTP#put): Sends a PUT request and returns a response # object. + # * [#query](rdoc-ref:Net::HTTP#query): Sends a QUERY request and returns a + # response object. # * [#request](rdoc-ref:Net::HTTP#request): Sends a request and returns a # response object. # * [#request_get](rdoc-ref:Net::HTTP#request_get): Sends a GET request and @@ -1689,6 +1693,27 @@ module Net # def put: (String path, String data, ?headers initheader) -> Net::HTTPResponse + # + # Sends a QUERY request to the server; returns an instance of a subclass of + # Net::HTTPResponse. + # + # The request is based on the Net::HTTP::Query object created from string + # `path`, string `data`, and initial headers hash `initheader`. + # + # data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}' + # http = Net::HTTP.new(hostname) + # http.query('/todos/1', data) # => # + # + # Related: + # + # * Net::HTTP::Query: request class for HTTP method QUERY. + # * Net::HTTP.query: sends QUERY request, returns response body. + # + def query: (String path, String data, ?headers initheader, ?bot dest) ?{ (String body_segment) -> void } -> Net::HTTPResponse + # + # Sends a QUERY request to the server; forms the response into a + # Net::HTTPResponse object. + # + # The request is based on the Net::HTTP::Query object created from string `path`, + # string `data`, and initial headers hash `initheader`. + # + # With no block given, returns the response object: + # + # http = Net::HTTP.new(hostname) + # http.query('/todos') + # # => # + # + # With a block given, calls the block with the response body and returns the + # response object: + # + # http.request_query('/todos') do |res| + # p res + # end # => # + # + # Output: + # + # # + # + def request_query: (String path, String data, ?headers initheader) ?{ (Net::HTTPResponse response) -> void } -> Net::HTTPResponse + #