我创建了一个 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 -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 规范将如下所示
parameters:
- in: body
name: config
schema:
type: object
$ref: '#/definitions/config'
关于go - Curl 因这个 swagger 规范而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66008945/