0

When sending an HTTP request with form values in x-www-form-url-encoded, it becomes a simple key-pair string separated by & characters.

So sending a form like this:

foo: bar
apple: red

Will result in the following string:

foo=bar&apple=red

Usually when sending an array, we use the same key, like this:

colors: [red, blue, green]

Becomes

colors=red&colors=blue&colors=green

But when an API expects an array in a request using this body and this type of data, is there a standard way of declaring that a property is an array with only one element, or with 0 elements?

Like this:

foo: [bar]

Or

thingsJonSnowKnows: []

2 Answers2

0

I suggest adding a separate key-value pair to store size. also, see How to pass an array within a query string? a possible solution is a comma-delimited value for array

YisraelU
  • 352
  • 1
  • 8
0

It depends a bit on your definition of standard, but there is a very common format, that's supported by quite a few different frameworks and languages. Creating an array with 1 item looks like this:

foo[]=bar

I don't think it's possible expressing an empty array, but you can create objects with this.

I believe this syntax originates from PHP, but I've seen this in Java and Javascript frameworks as well. I couldn't find an clear specification describing this exact format in the PHP docs.

Evert
  • 93,428
  • 18
  • 118
  • 189