英文:
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 "itemDescriptions=item 1" \
--data "itemDescriptions=item 2"
The following is not possible due to the restriction that an anonymous object cannot have the same key twice:
"https://example.com".PostUrlEncodedAsync(new {
itemDescriptions = "item 1",
itemDescriptions = "item 2"
});
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<KeyValuePair<string,string>>()
{
new KeyValuePair<string, string>("itemDescriptions", "item 1"),
new KeyValuePair<string, string>("itemDescriptions", "item 2")
};
"https://example.com".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<KeyValuePair<string, List<string>>>
似乎生成了正确的请求:
var formValues = new List<KeyValuePair<string, List<string>>>()
{
new KeyValuePair<string, List<string>>("itemDescriptions", new List<string> {"item 1", "item 2"}),
};
"https://example.com".PostUrlEncodedAsync(formValues);
现在请求中包含了这两个值。
英文:
Changing the type of the form values to List<KeyValuePair<string, List<string>>>
appears to generate the correct request:
var formValues = new List<KeyValuePair<string,List<string>>>()
{
new KeyValuePair<string, List<string>>("itemDescriptions", new List<string> {"item 1", "item 2"}),
};
"https://example.com".PostUrlEncodedAsync(formValues);
Both values appear in the request now.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论