7

Does anyone know how to send a field with brackets by a url encoded form using flurl?

Example: I want to send the foo[bar] field with the "foo" value like this

var response = await "https://server/request"
    .WithHeader("Header1", "headerValue")
    .PostUrlEncodedAsync(new
    {
         foo[bar] = "foo"
    })
Jose C
  • 317
  • 2
  • 13

1 Answers1

7

Brackets aren't allowed in C# identifiers, so using an anonymous object to represent a name/value pair won't work here. However, in all cases where Flurl parses an object to name/value pairs, it gives special treatment to dictionaries. So you can do this:

var response = await "https://server/request"
    .WithHeader("Header1", "headerValue")
    .PostUrlEncodedAsync(new Dictionary<string, string>()
    {
        { "foo[bar]", "foo" }
    });
Todd Menier
  • 37,557
  • 17
  • 150
  • 173