user1032531
user1032531

Reputation: 26281

Specifying an array to be sent as a GET parameter using Swagger/OpenAPI spec

The following swagger definition:

swagger: '2.0'
host: example.com
paths:
  /theResource:
    get:
      parameters:
        - $ref: '#/parameters/fields'
parameters:
  fields:
    name: fields
    description: Fields to return
    in: query
    type: array
    collectionFormat: multi
    uniqueItems: true
    items:
      type: string
      enum:
        - column1
        - column2
        - column3

Will generate the following URL, and the server will only receive fields equal to column3.

http://example.com/theResource?fields=column1&fields=column2&fields=column3

How do I change it so that it sends an array in the parameter? For instance:

http://example.com/theResource?fields[]=column1&fields[]=column2&fields[]=column3

Upvotes: 1

Views: 479

Answers (1)

Andrzej Budzanowski
Andrzej Budzanowski

Reputation: 1022

In PHP you can use http_build_query, that will construct proper query part of URL for you.

<?php

$query = http_build_query([
    'fields' => [
        'one', 'two', 'three'
    ],
]);

$url = "https://example.com/?".$query;

And yes, the fields[] syntax should do the trick.

Upvotes: 1

Related Questions