如何通过 API 网关传递路径变量
How to pass a path variable through API Gateway
这是我目前在 Swagger 文件中的内容:
"/v1/user/{username}": {
"get": {
"consumes": ["application/json"],
"produces": ["application/json"],
"parameters": [{
"name": "username",
"in": "path",
"description": "Username of user",
"required": true,
"type": "string"
}],
"responses": {
"200": {
"description": "user retrieved",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/User"
}
}
}
},
"x-amazon-apigateway-integration": {
"type": "http",
"uri": "http://useroregon.recopspipeline.com/v1/user/{username}",
"httpMethod": "GET",
"responses": {
"default": {
"statusCode": "200",
"responseTemplates": {
"application/json": "$input.json('$.body')"
}
}
}
}
},
但我的后端只收到“{username}”作为路径变量。
有人知道如何发送路径变量吗?
有关如何在 swagger json 文件中或通过 GUI 执行此操作的提示会很棒!
这个 OpenAPI (fka.Swagger) 文件缺少请求参数和代理服务之间的映射(requestParameters
in x-amazon-apigateway-integration
)。
"x-amazon-apigateway-integration": {
"type": "http",
"uri": "http://useroregon.recopspipeline.com/v1/user/{username}",
"requestParameters": {
"integration.request.path.username": "method.request.path.username"
}
"httpMethod": "GET",
"responses": {
"default": {
"statusCode": "200",
"responseTemplates": {
"application/json": "$input.json('$.body')"
}
}
}
}
如果您使用 API 网关控制台通过代理操作设计您的 API 并将其导出,您会得到:
swagger: "2.0"
info:
title: "Proxy"
schemes:
- "https"
paths:
/test/{username}:
get:
produces:
- "application/json"
parameters:
- name: "username"
in: "path"
required: true
type: "string"
responses:
200:
description: "200 response"
schema:
$ref: "#/definitions/Empty"
x-amazon-apigateway-integration:
responses:
default:
statusCode: "200"
requestParameters:
integration.request.path.username: "method.request.path.username"
passthroughBehavior: "when_no_match"
httpMethod: "GET"
uri: "http://somedomain.com/{username}"
type: "http"
definitions:
Empty:
type: "object"
您的定义也缺少 passthroughBehavior: "when_no_match"
但这可能不是问题。
这是我目前在 Swagger 文件中的内容:
"/v1/user/{username}": {
"get": {
"consumes": ["application/json"],
"produces": ["application/json"],
"parameters": [{
"name": "username",
"in": "path",
"description": "Username of user",
"required": true,
"type": "string"
}],
"responses": {
"200": {
"description": "user retrieved",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/User"
}
}
}
},
"x-amazon-apigateway-integration": {
"type": "http",
"uri": "http://useroregon.recopspipeline.com/v1/user/{username}",
"httpMethod": "GET",
"responses": {
"default": {
"statusCode": "200",
"responseTemplates": {
"application/json": "$input.json('$.body')"
}
}
}
}
},
但我的后端只收到“{username}”作为路径变量。
有人知道如何发送路径变量吗?
有关如何在 swagger json 文件中或通过 GUI 执行此操作的提示会很棒!
这个 OpenAPI (fka.Swagger) 文件缺少请求参数和代理服务之间的映射(requestParameters
in x-amazon-apigateway-integration
)。
"x-amazon-apigateway-integration": {
"type": "http",
"uri": "http://useroregon.recopspipeline.com/v1/user/{username}",
"requestParameters": {
"integration.request.path.username": "method.request.path.username"
}
"httpMethod": "GET",
"responses": {
"default": {
"statusCode": "200",
"responseTemplates": {
"application/json": "$input.json('$.body')"
}
}
}
}
如果您使用 API 网关控制台通过代理操作设计您的 API 并将其导出,您会得到:
swagger: "2.0"
info:
title: "Proxy"
schemes:
- "https"
paths:
/test/{username}:
get:
produces:
- "application/json"
parameters:
- name: "username"
in: "path"
required: true
type: "string"
responses:
200:
description: "200 response"
schema:
$ref: "#/definitions/Empty"
x-amazon-apigateway-integration:
responses:
default:
statusCode: "200"
requestParameters:
integration.request.path.username: "method.request.path.username"
passthroughBehavior: "when_no_match"
httpMethod: "GET"
uri: "http://somedomain.com/{username}"
type: "http"
definitions:
Empty:
type: "object"
您的定义也缺少 passthroughBehavior: "when_no_match"
但这可能不是问题。