Subterfuge-Revived / Remake-Backend

Server side validation and API
Creative Commons Zero v1.0 Universal
10 stars 1 forks source link

Comma-separated array values in x-www-form-urlencoded requests #21

Closed griendt closed 4 years ago

griendt commented 4 years ago

Enhancement Description

At the moment there is only one way to properly pass an array of values in a x-www-form-urlencoded Content-Type request and that is to add a key-value pair for each value you want to add, e.g.

participants[] => 3
participants[] => 4

This is apparently common across this content type and Laravel parses them properly. However, we may want to be able to pass just one comma-separated value in the request, like so:

participants[] => 3,4

Solution

explode the value belonging to a key if that key is an array-like. The problem is however that this is not known a priori. Laravel translates the keys as in the first example to participants.0 and participants.1 which are then no longer arrays themselves. Therefore it seems impossible to implement this in a middleware.

However, of course we could do an explode in the relevant controller whenever needed. We could then pass an argument as if it were not an array at all, like so:

participants => 3,4,5

This would be the easiest solution, but it also makes it a bit less obvious in Postman that the field is supposed to be an array.

QuinnBast commented 4 years ago

I think a single non-array field that is comma-separated is probably the most universal. Adding a key for every individual value isn't very intuitive imo.

Postman does allow for providing a description of the field as well as allows providing example requests so as long as the field has a description with something like "A comma separated list of player ids. ex: 1,2,3" then I think it should be fine.

griendt commented 4 years ago

Closing this issue as it is being discussed in more detail in #28.