elixir-plug / plug

Compose web applications with functions
https://hex.pm/packages/plug
Other
2.84k stars 582 forks source link

add an encoding option for parameters with multiple values without the square brackets #1096

Closed Sed9yDataBank closed 2 years ago

Sed9yDataBank commented 2 years ago

Feature Request

I am using tesla + plug to POST some data to an image processing API.

I am using this to encode the URL

plug Tesla.Middleware.FormUrlencoded,
    encode: &Plug.Conn.Query.encode/1,
    decode: &Plug.Conn.Query.decode/1

This is what I am sending green=yes&blue=no&shades[]=300&shades[]=199

%{
    "shades" => [300, 199],
    "green" => "yes,
    "blue" => "no"
  }

Notice how I have the same parameter shades with two values 300 and 199

Unfortunately the image processing API returns 'shades' is missing

The parameter is shades not shades[] so I should be sending green=yes&blue=no&shades=300&shades=199 without the square brackets.

Along side to This https://github.com/elixir-plug/plug/blob/dc20b7639157eae9be428ce032e69b2f3a3cfbef/lib/plug/conn/query.ex#L241, Shouldn't there be an option to support the above use case?

josevalim commented 2 years ago

I am not sure how frequent such encoding is, so my recommendation for now is to either encode it by hand or have your own version of the encoding functionality. :) Thank you!

Sed9yDataBank commented 2 years ago

Got it! thanks