aman
aman

Reputation: 338

Curl failing with this swagger spec

I have created a swagger spec

/config/download:
 post:
  summary: Download config
  consumes:
    - application/json
  produces:
    - application/zip
  parameters:
    - in: body
      name: config
      schema:
        type: array
        items:
          title: config files
          description: All config file name that need to be downloaded
          type: string
        minItems: 0
        maxItems: 1024
  responses:
    200:
      description: 
      schema:
        type: string
        format: binary

I am compiling this swagger spec by go-swagger and I have implemented the backend for this also

But when I try to curl this request I am getting

 curl -i  http://127.0.0.1:<port>/config/download -X "POST" -d '{"config": ["config1.json", "config.json"]}' -H "Content-Type: application/json"
HTTP/1.1 400 Bad Request
Content-Type: application/json
Date: Tue, 02 Feb 2021 07:36:06 GMT
Content-Length: 132
Connection: close

{"code":400,"message":"parsing config body from \"\" failed, because json: cannot unmarshal object into Go value of type []string"}

I don't think there is some problem with curl but swagger spec also seems to be correct, then what could be the issue. Its clear from curl error message that server is not processing this request correctly and its not even reaching the backend implementation code.

Any suggestion is appreciated.

Upvotes: 2

Views: 976

Answers (2)

Aman Chourasiya
Aman Chourasiya

Reputation: 1228

You should create a new definition for your request body parameter something like

config:
    title: config names
    properties:
      configs:
        title: config names
        description: List of config files to download.
        type: array
        items:
          title: Config
          type: string
        minItems: 0
        maxItems: 1024

And then swagger spec would look like this

       parameters:
        - in: body
          name: config
          schema:
            type: object
            $ref: '#/definitions/config'

Upvotes: 3

Matteo
Matteo

Reputation: 39380

Try adding a body element of type object in the response definition as follow:

  parameters:
  - name: body
    in: body
    type: object
    required: true
    properties:
      config:
        type: array
        items:
          title: config files
          description: All config file name that need to be downloaded
          type: string
        minItems: 0
        maxItems: 1024
  responses:

Upvotes: 1

Related Questions