Swagger-ui: Swagger send cut params to server

Created on 14 Sep 2018  路  8Comments  路  Source: swagger-api/swagger-ui

Hi there.
Please help ))
When I send from swagger UI request to server with params {
"address": "0xb4016d8ca33ab5970b1acdc3fb9a63a123a30638",
"tokens": "ZRX",
"tokens":"OMG"
}
the server receives data:
{
"address": "0xb4016d8ca33ab5970b1acdc3fb9a63a123a30638",
"tokens":"OMG"
}
or I send {
"address": "0xb4016d8ca33ab5970b1acdc3fb9a63a123a30638",
"tokens": "ZRX",
"tokens":"OMG",
"tokens":"TUSD",
"tokens":"POWR",
"tokens":"KNC"
}
and the server receives next data:
{
"address": "0xb4016d8ca33ab5970b1acdc3fb9a63a123a30638",
"tokens":"KNC"
}
So params tokens are cut in Swagger. How can I check this problem?
Code in yaml file:
/api/:
tags:
- Get balances of tokens
post:
summary: Balance of tokens
consumes:
- text/plain
parameters:
- in: body
name: Token List Balance
description: Token List Balance
schema:
type: object
required:
- address
- tokens
properties:
address:
type: string
default: "0xb4016d8ca33ab5970b1acdc3fb9a63a123a30638"
tokens:
type: string
enum: ['ZRX', 'OMG', 'ZIL', 'REP', 'GNT', 'SNT', 'BNT', 'MCO', 'KNC', 'POWR', 'TUSD']
responses:
200:
description: Success!
500:
description: Service unavialable

lock-bot

Most helpful comment

Your Postman example is not JSON, it's form data (x-www-form-urlencoded):

address=...&tokens=FOO&tokens=BAR

In OpenAPI/Swagger this is described as:

paths:
  /api/:
    post:
      consumes:
        - application/x-www-form-urlencoded
      parameters:
        - in: formData
          name: address
          type: string
          x-example: '0xb4016d8ca33ab5970b1acdc3fb9a63a123a30638'
        - in: formData
          name: tokens
          type: array
          items:
            type: string
            enum: ['ZRX', 'OMG', 'ZIL', 'REP', 'GNT', 'SNT', 'BNT', 'MCO', 'KNC', 'POWR', 'TUSD']
          collectionFormat: multi
      responses:
        200:
          description: OK

All 8 comments

image

server receive :
{ address: '0xb4016d8ca33ab5970b1acdc3fb9a63a123a30638',
tokens: 'OMG' }

You cannot have multiple tokens keys in the payload because JSON does not allow duplicate keys. Use https://jsonlint.com to check your JSON for syntax errors.

Does your server expect something like this maybe?

{
  "address": "0xb4016d8ca33ab5970b1acdc3fb9a63a123a30638",
  "tokens": ["ZRX", "OMG", "ZIL"]   // <------
}

In that case, tokens should be defined as an array:

tokens:
  type: array
  items:
    type: string
    enum: ['ZRX', 'OMG', 'ZIL', 'REP', 'GNT', 'SNT', 'BNT', 'MCO', 'KNC', 'POWR', 'TUSD']

But when I send from Postman it's OK
image

You cannot have multiple tokens keys in the payload because JSON does not allow duplicate keys. Use https://jsonlint.com to check your JSON for syntax errors.

Does your server expect something like this maybe?

{
  "address": "0xb4016d8ca33ab5970b1acdc3fb9a63a123a30638",
  "tokens": ["ZRX", "OMG", "ZIL"]   // <------
}

In that case, tokens should be defined as an array:

tokens:
  type: array
  items:
    type: string
    enum: ['ZRX', 'OMG', 'ZIL', 'REP', 'GNT', 'SNT', 'BNT', 'MCO', 'KNC', 'POWR', 'TUSD']

When I send form from postman server receive valid data
image

Your Postman example is not JSON, it's form data (x-www-form-urlencoded):

address=...&tokens=FOO&tokens=BAR

In OpenAPI/Swagger this is described as:

paths:
  /api/:
    post:
      consumes:
        - application/x-www-form-urlencoded
      parameters:
        - in: formData
          name: address
          type: string
          x-example: '0xb4016d8ca33ab5970b1acdc3fb9a63a123a30638'
        - in: formData
          name: tokens
          type: array
          items:
            type: string
            enum: ['ZRX', 'OMG', 'ZIL', 'REP', 'GNT', 'SNT', 'BNT', 'MCO', 'KNC', 'POWR', 'TUSD']
          collectionFormat: multi
      responses:
        200:
          description: OK

Your Postman example is not JSON, it's form data (x-www-form-urlencoded):

address=...&tokens=FOO&tokens=BAR

In OpenAPI/Swagger this is described as:

paths:
  /api/:
    post:
      consumes:
        - application/x-www-form-urlencoded
      parameters:
        - in: formData
          name: address
          type: string
          x-example: '0xb4016d8ca33ab5970b1acdc3fb9a63a123a30638'
        - in: formData
          name: tokens
          type: array
          items:
            type: string
            enum: ['ZRX', 'OMG', 'ZIL', 'REP', 'GNT', 'SNT', 'BNT', 'MCO', 'KNC', 'POWR', 'TUSD']
          collectionFormat: multi
      responses:
        200:
          description: OK

It is wonderful. Thank you! Everything is working

Closing as resolved 馃槃

Thanks for the help, @hkosova!

Was this page helpful?
0 / 5 - 0 ratings