Search code examples
gocurlswaggerswagger-2.0go-swagger

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.


Solution

  • 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'