使用Flurl,我如何传递具有相同键的多个URL编码表单值?

huangapple go评论94阅读模式
英文:

With flurl how can I pass multiple URL encoded form values with the same key?

问题

我想要复制以下的Curl请求,在这个请求中,我传递了多个具有相同键的表单参数,但是我想在C#中使用flurl来实现:

  1. curl -X POST \
  2. https://example.com \
  3. --data "itemDescriptions=item 1" \
  4. --data "itemDescriptions=item 2";

由于匿名对象不能具有相同的键两次的限制,以下操作是不可行的:

  1. "https://example.com".PostUrlEncodedAsync(new {
  2. itemDescriptions = "item 1",
  3. itemDescriptions = "item 2"
  4. });

我已经尝试了此Flurl问题中提到的所谓解决方法,但即使在参数的名称中没有使用[],也不能正常工作,而且我的服务器也不接受这种语法:

  1. var formValues = new List<KeyValuePair<string, string>>()
  2. {
  3. new KeyValuePair<string, string>("itemDescriptions", "item 1"),
  4. new KeyValuePair<string, string>("itemDescriptions", "item 2")
  5. };
  6. "https://example.com".PostUrlEncodedAsync(formValues);

使用这种方法,只有列表中的最后一个被发送到请求,而不是两者都被发送。

英文:

I would like to replicate the following curl request where I pass in multiple form parameters with the same key, but using flurl in C#.

  1. curl -X POST \
  2. https://example.com \
  3. --data &quot;itemDescriptions=item 1&quot; \
  4. --data &quot;itemDescriptions=item 2&quot;

The following is not possible due to the restriction that an anonymous object cannot have the same key twice:

  1. &quot;https://example.com&quot;.PostUrlEncodedAsync(new {
  2. itemDescriptions = &quot;item 1&quot;,
  3. itemDescriptions = &quot;item 2&quot;
  4. });

I have tried the following supposed workaround from this Flurl issue but it doesn't work even without the [] at the name of the parameter, but also my server doesn't accept them with that syntax:

  1. var formValues = new List&lt;KeyValuePair&lt;string,string&gt;&gt;()
  2. {
  3. new KeyValuePair&lt;string, string&gt;(&quot;itemDescriptions&quot;, &quot;item 1&quot;),
  4. new KeyValuePair&lt;string, string&gt;(&quot;itemDescriptions&quot;, &quot;item 2&quot;)
  5. };
  6. &quot;https://example.com&quot;.PostUrlEncodedAsync(formValues);

With this I only end up with the last one in the list being sent in the request instead of both...

答案1

得分: 0

将表单值的类型更改为List&lt;KeyValuePair&lt;string, List&lt;string&gt;&gt;&gt; 似乎生成了正确的请求:

  1. var formValues = new List&lt;KeyValuePair&lt;string, List&lt;string&gt;&gt;&gt;()
  2. {
  3. new KeyValuePair&lt;string, List&lt;string&gt;&gt;(&quot;itemDescriptions&quot;, new List&lt;string&gt; {&quot;item 1&quot;, &quot;item 2&quot;}),
  4. };
  5. &quot;https://example.com&quot;.PostUrlEncodedAsync(formValues);

现在请求中包含了这两个值。

英文:

Changing the type of the form values to List&lt;KeyValuePair&lt;string, List&lt;string&gt;&gt;&gt; appears to generate the correct request:

  1. var formValues = new List&lt;KeyValuePair&lt;string,List&lt;string&gt;&gt;&gt;()
  2. {
  3. new KeyValuePair&lt;string, List&lt;string&gt;&gt;(&quot;itemDescriptions&quot;, new List&lt;string&gt; {&quot;item 1&quot;, &quot;item 2&quot;}),
  4. };
  5. &quot;https://example.com&quot;.PostUrlEncodedAsync(formValues);

Both values appear in the request now.

huangapple
  • 本文由 发表于 2023年2月14日 21:31:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定