我们有一个API网关,移动的应用程序请求在到达我们的核心API之前通过它进行代理,我们有一个Web应用程序直接到达核心API。
我们只想区分哪些请求来自移动的应用程序。我们不想更改和重新提交移动应用程序。因此,我想将标头硬编码到API网关中,例如"X-IS-MOBILE": "true"
我第一次尝试在参数中添加它:
swagger: "2.0"
info:
version: "2019-07-22T10:33:53Z"
title: "Mobile API Integration"
host: "mobile.domain.link"
basePath: "/v3"
schemes:
- "https"
paths:
/app-info:
post:
operationId: "appInfo"
consumes:
- "application/json"
produces:
- "application/json"
parameters:
- in: header
name: "X-IS-MOBILE"
type: boolean
default: true
- in: "body"
name: "AppInfoPayload"
required: true
schema:
$ref: "#/definitions/AppInfoPayload"
responses:
"200":
description: "200 response"
schema:
$ref: "#/definitions/AppInfoView"
headers:
Access-Control-Allow-Origin:
type: "string"
Access-Control-Allow-Headers:
type: "string"
"400":
description: "400 response"
schema:
$ref: "#/definitions/ApiError"
"401":
description: "401 response"
schema:
$ref: "#/definitions/ApiError"
"500":
description: "500 response"
schema:
$ref: "#/definitions/ApiError"
x-amazon-apigateway-integration:
httpMethod: "POST"
uri: "https://api.domain.link/v1/app-info"
responses:
default:
statusCode: "200"
passthroughBehavior: "when_no_match"
type: "http_proxy"
options:
produces:
- "application/json"
responses:
"200":
description: "200 response"
schema:
$ref: "#/definitions/Empty"
headers:
Access-Control-Allow-Origin:
type: "string"
Access-Control-Allow-Methods:
type: "string"
Access-Control-Allow-Headers:
type: "string"
x-amazon-apigateway-integration:
httpMethod: "OPTIONS"
uri: "https://api.domain.link/v1/app-info"
responses:
default:
statusCode: "200"
passthroughBehavior: "when_no_match"
type: "http_proxy"
这不起作用。当我检查核心API日志时,标头中没有X-IS-MOBILE
。
然后我尝试在x-amazon-apigateway-integration
中使用requestTemplates
:
swagger: "2.0"
info:
version: "2019-07-22T10:33:53Z"
title: "Mobile API Integration"
host: "mobile.domain.link"
basePath: "/v3"
schemes:
- "https"
paths:
/app-info:
post:
operationId: "appInfo"
consumes:
- "application/json"
produces:
- "application/json"
parameters:
- in: "body"
name: "AppInfoPayload"
required: true
schema:
$ref: "#/definitions/AppInfoPayload"
responses:
"200":
description: "200 response"
schema:
$ref: "#/definitions/AppInfoView"
headers:
Access-Control-Allow-Origin:
type: "string"
Access-Control-Allow-Headers:
type: "string"
"400":
description: "400 response"
schema:
$ref: "#/definitions/ApiError"
"401":
description: "401 response"
schema:
$ref: "#/definitions/ApiError"
"500":
description: "500 response"
schema:
$ref: "#/definitions/ApiError"
x-amazon-apigateway-integration:
httpMethod: "POST"
uri: "https://api.domain.link/v1/app-info"
requestTemplates:
'application/json': |
{
"headers": {
"X-IS-MOBILE": "true"
},
"body": $input.json('$')
}
responses:
default:
statusCode: "200"
passthroughBehavior: "when_no_match"
type: "http_proxy"
options:
produces:
- "application/json"
responses:
"200":
description: "200 response"
schema:
$ref: "#/definitions/Empty"
headers:
Access-Control-Allow-Origin:
type: "string"
Access-Control-Allow-Methods:
type: "string"
Access-Control-Allow-Headers:
type: "string"
x-amazon-apigateway-integration:
httpMethod: "OPTIONS"
uri: "https://api.domain.link/v1/app-info"
responses:
default:
statusCode: "200"
passthroughBehavior: "when_no_match"
type: "http_proxy"
相关代码为:
requestTemplates:
'application/json': |
{
"headers": {
"X-IS-MOBILE": "true"
},
"body": $input.json('$')
}
同样,这也不起作用,核心API日志中没有X-IS-移动的。我做错了什么?
1条答案
按热度按时间pftdvrlh1#
因此,在第一次尝试中,您将
X-IS-MOBILE
头添加到incomingrequestheader参数中以进行验证(即,如果需要但不存在,APIGW将使用400 Bad Request
进行回复)。在第二次尝试时,
requestTemplates
存在一些问题:1.不适用于
http_proxy
集成类型,仅适用于http
;1.code 本身不正确,应为:
参见相关文档和示例here。
但是,如果您想要/需要使用
http_proxy
集成类型,您仍然可以将标题Map添加到集成请求中,而使用requestParameters
: