在 AWS API Gateway 的请求映射模板中,JSON 格式默认使用双引号。然而,在某些情况下,我们可能需要使用单引号来表示字符串。
为了以单引号形式编写 JSON,我们可以使用高级变量映射功能。具体来说,我们需要使用 $util.parseJson() 方法将原始请求正文解析为 JSON,并使用 $util.escapeJavaScript() 方法将双引号转换为单引号。
以下是将 API 网关请求映射格式为单引号 JSON 的示例:
#set($rawBody = $input.body) #set($jsonBody = $util.parseJson($rawBody)) { "orderId": "$jsonBody.order_id", "customerName": "$jsonBody.customer_name", "shippingAddress": "$util.escapeJavaScript($jsonBody.shipping_address)" }
在此示例中,我们使用 $util.parseJson() 将原始请求正文解析为 JSON,并使用 $util.escapeJavascript() 将 shipping_address 属性中的双引号转换为单引号。这样,我们就可以在 API 网关请求映射中使用带单引号的 JSON 格式。
上一篇:API网关请求验证不起作用