[英]Hardcode a header into AWS API Gateway
我們有一個 API 網關,移動應用程序請求在到達我們的核心 API 之前通過它進行處理。我們有一個直接進入核心 API 的網絡應用程序。
我們只是想區分哪些請求來自移動應用程序。 我們不想更改並重新提交移動應用程序。 因此,我想將 header 硬編碼到 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-MOBILE。 我究竟做錯了什么?
因此,在您的第一次嘗試中,您將X-IS-MOBILE
header 添加到傳入請求標頭參數以進行驗證(即,如果需要但不存在,APIGW 將回復400 Bad Request
)。
在第二次嘗試時, requestTemplates
有幾個問題:
http_proxy
集成類型,僅適用於http
; x-amazon-apigateway-integration:
httpMethod: "POST"
uri: "https://api.domain.link/v1/app-info"
requestTemplates:
'application/json': |
#set($context.requestOverride.header.x-is-mobile = 'true')
$input.json('$')
responses:
default:
statusCode: "200"
passthroughBehavior: "when_no_match"
type: "http"
請參閱此處的相關文檔和示例。
但是,如果您想要/需要使用http_proxy
集成類型,您仍然可以將 header 映射添加到集成請求中,而不是使用requestParameters
:
x-amazon-apigateway-integration:
httpMethod: "POST"
uri: "https://api.domain.link/v1/app-info"
requestParameters:
integration.request.header.x-is-mobile: "'true'"
responses:
default:
statusCode: "200"
passthroughBehavior: "when_no_match"
type: "http_proxy"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.