@@ -806,56 +806,108 @@ def set_form_data(params, sep = '&')
806
806
807
807
alias form_data = set_form_data
808
808
809
- # Set an HTML form data set.
810
- # +params+ :: The form data to set, which should be an enumerable.
811
- # See below for more details.
812
- # +enctype+ :: The content type to use to encode the form submission,
813
- # which should be application/x-www-form-urlencoded or
814
- # multipart/form-data.
815
- # +formopt+ :: An options hash, supporting the following options:
816
- # :boundary :: The boundary of the multipart message. If
817
- # not given, a random boundary will be used.
818
- # :charset :: The charset of the form submission. All
819
- # field names and values of non-file fields
820
- # should be encoded with this charset.
821
- #
822
- # Each item of params should respond to +each+ and yield 2-3 arguments,
823
- # or an array of 2-3 elements. The arguments yielded should be:
824
- #
825
- # - The name of the field.
826
- # - The value of the field, it should be a String or a File or IO-like.
827
- # - An options hash, supporting the following options
828
- # (used only for file uploads); entries:
829
- #
830
- # - +:filename+: The name of the file to use.
831
- # - +:content_type+: The content type of the uploaded file.
832
- #
833
- # Each item is a file field or a normal field.
834
- # If +value+ is a File object or the +opt+ hash has a :filename key,
835
- # the item is treated as a file field.
836
- #
837
- # If Transfer-Encoding is set as chunked, this sends the request using
838
- # chunked encoding. Because chunked encoding is HTTP/1.1 feature,
839
- # you should confirm that the server supports HTTP/1.1 before using
840
- # chunked encoding.
841
- #
842
- # Example:
843
- #
844
- # req.set_form([["q", "ruby"], ["lang", "en"]])
845
- #
846
- # req.set_form({"f"=>File.open('/path/to/filename')},
847
- # "multipart/form-data",
848
- # charset: "UTF-8",
849
- # )
850
- #
851
- # req.set_form([["f",
852
- # File.open('/path/to/filename.bar'),
853
- # {filename: "other-filename.foo"}
854
- # ]],
855
- # "multipart/form-data",
856
- # )
857
- #
858
- # See also RFC 2388, RFC 2616, HTML 4.01, and HTML5
809
+ # Stores form data to be used in a +POST+ or +PUT+ request.
810
+ #
811
+ # The form data given in +params+ consists of zero or more fields;
812
+ # each field is:
813
+ #
814
+ # - A scalar value.
815
+ # - A name/value pair.
816
+ # - An IO stream opened for reading.
817
+ #
818
+ # Argument +params+ should be an
819
+ # {Enumerable}[https://docs.ruby-lang.org/en/master/Enumerable.html#module-Enumerable-label-Enumerable+in+Ruby+Classes]
820
+ # (method <tt>params.map</tt> will be called),
821
+ # and is often an array or hash.
822
+ #
823
+ # First, we set up a request:
824
+ #
825
+ # _uri = uri.dup
826
+ # _uri.path ='/posts'
827
+ # req = Net::HTTP::Post.new(_uri)
828
+ #
829
+ # <b>Argument +params+ As an Array</b>
830
+ #
831
+ # When +params+ is an array,
832
+ # each of its elements is a subarray that defines a field;
833
+ # the subarray may contain:
834
+ #
835
+ # - One string:
836
+ #
837
+ # req.set_form([['foo'], ['bar'], ['baz']])
838
+ #
839
+ # - Two strings:
840
+ #
841
+ # req.set_form([%w[foo 0], %w[bar 1], %w[baz 2]])
842
+ #
843
+ # - When argument +enctype+ (see below) is given as
844
+ # <tt>'multipart/form-data'</tt>:
845
+ #
846
+ # - A string name and an IO stream opened for reading:
847
+ #
848
+ # require 'stringio'
849
+ # req.set_form([['file', StringIO.new('Ruby is cool.')]])
850
+ #
851
+ # - A string name, an IO stream opened for reading,
852
+ # and an options hash, which may contain these entries:
853
+ #
854
+ # - +:filename+: The name of the file to use.
855
+ # - +:content_type+: The content type of the uploaded file.
856
+ #
857
+ # Example:
858
+ #
859
+ # req.set_form([['file', file, {filename: "other-filename.foo"}]]
860
+ #
861
+ # The various forms may be mixed:
862
+ #
863
+ # req.set_form(['foo', %w[bar 1], ['file', file]])
864
+ #
865
+ # <b>Argument +params+ As a Hash</b>
866
+ #
867
+ # When +params+ is a hash,
868
+ # each of its entries is a name/value pair that defines a field:
869
+ #
870
+ # - The name is a string.
871
+ # - The value may be:
872
+ #
873
+ # - +nil+.
874
+ # - Another string.
875
+ # - An IO stream opened for reading
876
+ # (only when argument +enctype+ -- see below -- is given as
877
+ # <tt>'multipart/form-data'</tt>).
878
+ #
879
+ # Examples:
880
+ #
881
+ # # Nil-valued fields.
882
+ # req.set_form({'foo' => nil, 'bar' => nil, 'baz' => nil})
883
+ #
884
+ # # String-valued fields.
885
+ # req.set_form({'foo' => 0, 'bar' => 1, 'baz' => 2})
886
+ #
887
+ # # IO-valued field.
888
+ # require 'stringio'
889
+ # req.set_form({'file' => StringIO.new('Ruby is cool.')})
890
+ #
891
+ # # Mixture of fields.
892
+ # req.set_form({'foo' => nil, 'bar' => 1, 'file' => file})
893
+ #
894
+ # Optional argument +enctype+ specifies the value to be given
895
+ # to field <tt>'Content-Type'</tt>, and must be one of:
896
+ #
897
+ # - <tt>'application/x-www-form-urlencoded'</tt> (the default).
898
+ # - <tt>'multipart/form-data'</tt>;
899
+ # see {RFC 7578}[https://www.rfc-editor.org/rfc/rfc7578].
900
+ #
901
+ # Optional argument +formopt+ is a hash of options
902
+ # (applicable only when argument +enctype+
903
+ # is <tt>'multipart/form-data'</tt>)
904
+ # that may include the following entries:
905
+ #
906
+ # - +:boundary+: The value is the boundary string for the multipart message.
907
+ # If not given, the boundary is a random string.
908
+ # See {Boundary}[https://www.rfc-editor.org/rfc/rfc7578#section-4.1].
909
+ # - +:charset+: Value is the character set for the form submission.
910
+ # Field names and values of non-file fields should be encoded with this charset.
859
911
#
860
912
def set_form ( params , enctype = 'application/x-www-form-urlencoded' , formopt = { } )
861
913
@body_data = params
0 commit comments