Request parses POST data contains array arg "improperly"?

Could you guys help me with this?

When I make post request which contains an array arg:

curl --location --request POST 'http://127.0.0.1:8080/push' \
  --form 'names[0]="name0"' \
  --form 'names[1]="name1"' \
  --form 'names[2]="name2"'

The request.form is as below:

I thought the request.form should be like this:

{
    'names': ['name0', 'name1', 'name2']
}

Thanks!

To stack them in an array, the correct format would be:

curl --location --request POST 'http://127.0.0.1:8080/push' \
  --form 'names="name0"' \
  --form 'names="name1"' \
  --form 'names="name2"'

Then you would get:

{'names': ['name0', 'name1', 'name2']}

You can see it in the docs here: Request | Sanic Framework

Maybe it could be more explicit that the same applies to form data.

Thanks for the reply.

Actually I make the request in a PHP backend.
Generally, the request would be like this:

$client = new Client([
    'baseUrl' => 'http://127.0.0.1:8080',
]);

$client->post('/push', [
    'names' => [
        'name0',
        'name1',
        'name2',
    ]
]);

This Client make a request equivalent to a curl command execution:

curl --location --request POST 'http://127.0.0.1:8080/push' \
  --form 'names[0]="name0"' \
  --form 'names[1]="name1"' \
  --form 'names[2]="name2"'

That’s why I post this question.

Maybe I can change the names to ‘name0,name1,name2’, or see if this Client can make a request like this:

curl --location --request POST 'http://127.0.0.1:8080/push' \
  --form 'names="name0"' \
  --form 'names="name1"' \
  --form 'names="name2"'

Thanks again!

Sorry for the late response. There are several methods on the Internet and no single pattern for how multiple arguments in a list should be passed via query arguments. Some may call this a failure of the protocol. To make a long story short, that is not the method that Sanic uses.

Instead, ALL query arguments are treated as a list in Sanic. This is much closer to the spirit of what the protocol provides (IMO).

Checkout the docs to learn more: Request | Sanic Framework

In short:

  1. Send ...?name=one&name=two
  2. Get request.args.getlist("name")