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

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

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

问题

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

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

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

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

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

var formValues = new List<KeyValuePair<string, string>>() 
{
    new KeyValuePair<string, string>("itemDescriptions", "item 1"),
    new KeyValuePair<string, string>("itemDescriptions", "item 2")
};
"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#.

curl -X POST \
  https://example.com \
  --data &quot;itemDescriptions=item 1&quot; \
  --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:

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

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:

var formValues = new List&lt;KeyValuePair&lt;string,string&gt;&gt;() 
{
    new KeyValuePair&lt;string, string&gt;(&quot;itemDescriptions&quot;, &quot;item 1&quot;),
    new KeyValuePair&lt;string, string&gt;(&quot;itemDescriptions&quot;, &quot;item 2&quot;)
};
&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; 似乎生成了正确的请求:

var formValues = new List&lt;KeyValuePair&lt;string, List&lt;string&gt;&gt;&gt;()
{
    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;}),
};

&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:

var formValues = new List&lt;KeyValuePair&lt;string,List&lt;string&gt;&gt;&gt;()
{
    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;}),
};

&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:

确定