如何在招摇中传递多值查询参数 [英] how to pass multi value query params in swagger
问题描述
我在 swagger.yml 中有以下服务.编写服务以便 page_id 可以多次传递.例如 /pages?page_id[]=123&page_id[]=542
I have following service in swagger.yml. The service is written so that page_id can be passed multiple times. e.g /pages?page_id[]=123&page_id[]=542
我检查了此链接 https://swagger.io/specification/ 但不明白如何更新yml 所以我可以多次传递 id.
I checked this link https://swagger.io/specification/ but couldnt understand how could i update yml so i could pass id multiple times.
我看到我必须设置 collectionFormat
但不知道如何设置.
I see that i have to set collectionFormat
but dont know how.
我尝试像下面这样更新它,但没有运气 https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md.
I tried updating it like below but no luck https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md.
它会生成类似 'http://localhost:0000/pages?page_id=123 的 url%2C%20542`
'/pages':
get:
tags:
-
summary: get the list of pages
operationId: getPages
produces:
- application/json
parameters:
- name: page_id
in: query
description: some description
required: false
type: string
collectionFormat: multi
- name: page_detail
in: query
description: some description
required: false
type: string
responses:
'200':
description: OK
'401':
description: Authentication Failed
'404':
description: Not Found
'503':
description: Service Not Available
推荐答案
你快到了.将参数命名为page_id[]
,使其为type:array
并使用collectionFormat:multi
:
You are almost there. Name the parameter page_id[]
, make it type: array
and use collectionFormat: multi
:
parameters:
- name: page_id[]
in: query
description: some description
required: false
type: array
items:
type: string # or type: integer or whatever the type is
collectionFormat: multi
请注意,请求将使用百分比编码为 %5B
和 %5D<的
[
和 ]
字符发送/code>,因为根据 RFC 3986,它们是保留字符.
Note that the requests will be sent with the [
and ]
characters percent-encoded as %5B
and %5D
, because they are reserved characters according to RFC 3986.
http://example.com/pages?page_id%5B%5D=123&page_id%5B%5D=456
这篇关于如何在招摇中传递多值查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!