Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
Net::HTTPHeader#form_data=
# File tmp/rubies/ruby-4.1.0-preview1/lib/net/http/header.rb, line 857
def set_form_data(params, sep = '&')
query = URI.encode_www_form(params)
query.gsub!(/&/, sep) if sep != '&'
self.body = query
self.content_type = 'application/x-www-form-urlencoded'
end
Alias of #set_form_data
Sets the request body to a URL-encoded string derived from argument params, and sets request header field 'Content-Type' to 'application/x-www-form-urlencoded'.
The resulting request is suitable for HTTP request POST or PUT.
Argument params must be suitable for use as argument enum to URI.encode_www_form.
With only argument params given, sets the body to a URL-encoded string with the default separator '&':
req = Net::HTTP::Post.new('example.com') req.set_form_data(q: 'ruby', lang: 'en') req.body # => "q=ruby&lang=en" req['Content-Type'] # => "application/x-www-form-urlencoded" req.set_form_data([['q', 'ruby'], ['lang', 'en']]) req.body # => "q=ruby&lang=en" req.set_form_data(q: ['ruby', 'perl'], lang: 'en') req.body # => "q=ruby&q=perl&lang=en" req.set_form_data([['q', 'ruby'], ['q', 'perl'], ['lang', 'en']]) req.body # => "q=ruby&q=perl&lang=en"
With string argument sep also given, uses that string as the separator:
req.set_form_data({q: 'ruby', lang: 'en'}, '|') req.body # => "q=ruby|lang=en"
Net::HTTPHeader#form_data= is an alias for Net::HTTPHeader#set_form_data.