Curl 因这个 swagger 规范而失败
Curl failing with this swagger spec
我创建了一个 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 错误消息中可以清楚地看出服务器没有正确处理这个请求,甚至没有到达后端实现代码。
欢迎任何建议。
尝试在响应定义中添加对象类型的正文元素,如下所示:
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:
您应该为您的请求正文参数创建一个新定义,例如
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'
我创建了一个 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 错误消息中可以清楚地看出服务器没有正确处理这个请求,甚至没有到达后端实现代码。
欢迎任何建议。
尝试在响应定义中添加对象类型的正文元素,如下所示:
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:
您应该为您的请求正文参数创建一个新定义,例如
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'