@@ -1596,8 +1596,7 @@ def edit_path(path)
1596
1596
# get(path, initheader = nil) {|res| ... }
1597
1597
#
1598
1598
# Sends a GET request to the server;
1599
- # returns a Net::HTTPResponse object,
1600
- # which actually will be an instance of a subclass of that class:
1599
+ # returns an instance of a subclass of Net::HTTPResponse.
1601
1600
#
1602
1601
# The request is based on the Net::HTTP::Get object
1603
1602
# created from string +path+ and initial headers hash +initheader+.
@@ -1631,57 +1630,81 @@ def get(path, initheader = nil, dest = nil, &block) # :yield: +body_segment+
1631
1630
res
1632
1631
end
1633
1632
1634
- # Gets only the header from +path+ on the connected-to host.
1635
- # +header+ is a Hash like { 'Accept' => '*/*', ... } .
1633
+ # Sends a HEAD request to the server;
1634
+ # returns an instance of a subclass of Net::HTTPResponse .
1636
1635
#
1637
- # This method returns a Net::HTTPResponse object.
1638
- #
1639
- # This method never raises an exception.
1636
+ # The request is based on the Net::HTTP::Head object
1637
+ # created from string +path+ and initial headers hash +initheader+.
1640
1638
#
1641
- # response = nil
1642
- # Net::HTTP.start('some.www.server', 80) {|http|
1643
- # response = http.head('/index.html')
1644
- # }
1645
- # p response['content-type']
1639
+ # res = http.head('/todos/1') # => #<Net::HTTPOK 200 OK readbody=true>
1640
+ # res.body # => nil
1641
+ # res.to_hash.take(3)
1642
+ # # =>
1643
+ # [["date", ["Wed, 15 Feb 2023 15:25:42 GMT"]],
1644
+ # ["content-type", ["application/json; charset=utf-8"]],
1645
+ # ["connection", ["close"]]]
1646
1646
#
1647
1647
def head ( path , initheader = nil )
1648
1648
request ( Head . new ( path , initheader ) )
1649
1649
end
1650
1650
1651
- # Posts +data+ (must be a String) to +path+. +header+ must be a Hash
1652
- # like { 'Accept' => '*/*', ... }.
1651
+ # :call-seq:
1652
+ # post(path, data, initheader = nil) {|res| ... }
1653
+ #
1654
+ # Sends a POST request to the server;
1655
+ # returns an instance of a subclass of Net::HTTPResponse.
1656
+ #
1657
+ # The request is based on the Net::HTTP::Post object
1658
+ # created from string +path+, string +data+, and initial headers hash +initheader+.
1659
+ #
1660
+ # With a block given, calls the block with the response body:
1653
1661
#
1654
- # This method returns a Net::HTTPResponse object.
1662
+ # data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
1663
+ # http.post('/todos', data) do |res|
1664
+ # p res
1665
+ # end # => #<Net::HTTPCreated 201 Created readbody=true>
1655
1666
#
1656
- # If called with a block, yields each fragment of the
1657
- # entity body in turn as a string as it is read from
1658
- # the socket. Note that in this case, the returned response
1659
- # object will *not* contain a (meaningful) body.
1667
+ # Output:
1660
1668
#
1661
- # +dest+ argument is obsolete.
1662
- # It still works but you must not use it.
1669
+ # "{\n \"{\\\"userId\\\": 1, \\\"id\\\": 1, \\\"title\\\": \\\"delectus aut autem\\\", \\\"completed\\\": false}\": \"\",\n \"id\": 201\n}"
1663
1670
#
1664
- # This method never raises exception.
1671
+ # With no block given, simply returns the response object:
1665
1672
#
1666
- # response = http.post('/cgi-bin/search.rb ', 'query=foo')
1673
+ # http.post('/todos ', data) # => #<Net::HTTPCreated 201 Created readbody=true>
1667
1674
#
1668
- # # using block
1669
- # File.open('result.txt', 'w') {|f|
1670
- # http.post('/cgi-bin/search.rb', 'query=foo') do |str|
1671
- # f.write str
1672
- # end
1673
- # }
1675
+ # Related:
1674
1676
#
1675
- # You should set Content-Type: header field for POST.
1676
- # If no Content-Type: field given, this method uses
1677
- # "application/x-www-form-urlencoded" by default.
1677
+ # - Net::HTTP::Post: request class for \HTTP method POST.
1678
+ # - Net::HTTP.post: sends POST request, returns response body.
1678
1679
#
1679
1680
def post ( path , data , initheader = nil , dest = nil , &block ) # :yield: +body_segment+
1680
1681
send_entity ( path , data , initheader , dest , Post , &block )
1681
1682
end
1682
1683
1683
- # Sends a PATCH request to the +path+ and gets a response,
1684
- # as an HTTPResponse object.
1684
+ # :call-seq:
1685
+ # patch(path, data, initheader = nil) {|res| ... }
1686
+ #
1687
+ # Sends a PATCH request to the server;
1688
+ # returns an instance of a subclass of Net::HTTPResponse.
1689
+ #
1690
+ # The request is based on the Net::HTTP::Patch object
1691
+ # created from string +path+, string +data+, and initial headers hash +initheader+.
1692
+ #
1693
+ # With a block given, calls the block with the response body:
1694
+ #
1695
+ # data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
1696
+ # http.patch('/todos/1', data) do |res|
1697
+ # p res
1698
+ # end # => #<Net::HTTPOK 200 OK readbody=true>
1699
+ #
1700
+ # Output:
1701
+ #
1702
+ # "{\n \"userId\": 1,\n \"id\": 1,\n \"title\": \"delectus aut autem\",\n \"completed\": false,\n \"{\\\"userId\\\": 1, \\\"id\\\": 1, \\\"title\\\": \\\"delectus aut autem\\\", \\\"completed\\\": false}\": \"\"\n}"
1703
+ #
1704
+ # With no block given, simply returns the response object:
1705
+ #
1706
+ # http.patch('/todos/1', data) # => #<Net::HTTPCreated 201 Created readbody=true>
1707
+ #
1685
1708
def patch ( path , data , initheader = nil , dest = nil , &block ) # :yield: +body_segment+
1686
1709
send_entity ( path , data , initheader , dest , Patch , &block )
1687
1710
end
0 commit comments