我已经创建了一个swagger规范
/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
我正在用go-swagger编译这个swagger规范,并且我已经为此实现了后端
但是当我尝试curl这个请求时,我得到了
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"}
我不认为curl有什么问题,但是swagger规范似乎也是正确的,那么问题是什么呢?从curl错误消息中可以清楚地看出,服务器没有正确处理此请求,甚至没有到达后端实现代码。
欢迎提出任何建议。
您应该为您的请求体参数创建一个新的定义,如
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
然后swagger spec看起来像这样
parameters:
- in: body
name: config
schema:
type: object
$ref: '#/definitions/config'
尝试在响应定义中添加object类型的body元素:
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: