如何发送带有 POST 请求的数组,该数组的参数名称包含一组空的方括号 []?

How to send an array with a POST request, with a parameter name for the array that contains an empty set of square brackets []?

因此,Rails 通常处理通过 HTTP Post 请求(表单)发送的传入数组的解析,如下所示:

"Normally Rails ignores duplicate parameter names. If the parameter name contains an empty set of square brackets [] then they will be accumulated in an array." - Rails Guides

但是当使用 Net::HTTP.Post 向第三方服务 (API) 发送 Post 请求时,似乎这种处理数组的约定在未遵循 HTTP Post 请求。

此代码:

data = {:categories => [one, two, three]}
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(data)
response = http.request(request)

然后 set_form_data 将像这样序列化数组:

categories=one&categories=two&categories=three

而不是像这样(我认为这是传统的 Rails 方式):

categories[]=one&categories[]=two&categories[]=three

为什么?

我可以看出它与最近实施的 URI.encode_www_form 方法有关 set_form_data 使用。但是偏离常规Rails方式的目的是什么?

而且,更重要的是,我如何轻松修改它以以后一种方式发送它(无需重载一堆固有的 Ruby/Rails 方法)?

我发现解决方案就像更改 table 名称一样简单:

data = {'categories[]' => [one, two, three]}

即使数据哈希的其他元素是 :symbols

我仍然很想知道为什么 Rails 在使用 Net::HTTPHeader::set_form_data 方法时需要这个 "hack",以获得 Rails' 否则传统的方法处理 url 参数中的数组。