首页 文章

如何在AWS API Gateway中预先定义URL传递URL查询字符串参数

提问于
浏览
1

有没有办法在方法请求和集成请求中定义URL查询字符串参数,并将查询字符串传递给HTTP代理?

例如,在https:// -api.us-east-1.amazonaws.com/test/user?start=1&end=10的情况下

查询字符串1.开始2.结束

如果我将URL查询字符串参数放在方法请求和集成请求中,那么我就可以获得两个查询字符串 . 但是,如果我不添加它们并在postman中发出请求,那么在后端我没有得到两个查询字符串 .

我想在没有在方法请求和集成请求中定义的情况下获取查询字符串 . 原因是如果有很多,我不想手动输入所有参数 .

有没有办法做到这一点?

我正在使用HTTP代理,这是我正在使用的模板映射 . (它实际上是方法请求直通下拉) .

#set($allParams = $input.params())
{
"body-json" : "$input.json('$')",
"params" : {
#foreach($type in $allParams.keySet())
    #set($params = $allParams.get($type))
"$type" : {
    #foreach($paramName in $params.keySet())
    "$paramName" : "$util.escapeJavaScript($params.get($paramName))"
        #if($foreach.hasNext),#end
    #end
}
    #if($foreach.hasNext),#end
#end
},
"stage-variables" : {
#foreach($key in $stageVariables.keySet())
"$key" : "$util.escapeJavaScript($stageVariables.get($key))"
    #if($foreach.hasNext),#end
#end
},
"context" : {
    "account-id" : "$context.identity.accountId",
    "api-id" : "$context.apiId",
    "api-key" : "$context.identity.apiKey",
    "authorizer-principal-id" : "$context.authorizer.principalId",
    "caller" : "$context.identity.caller",
    "cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider",
    "cognito-authentication-type" : "$context.identity.cognitoAuthenticationType",
    "cognito-identity-id" : "$context.identity.cognitoIdentityId",
    "cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId",
    "http-method" : "$context.httpMethod",
    "stage" : "$context.stage",
    "source-ip" : "$context.identity.sourceIp",
    "user" : "$context.identity.user",
    "user-agent" : "$context.identity.userAgent",
    "user-arn" : "$context.identity.userArn",
    "request-id" : "$context.requestId",
    "resource-id" : "$context.resourceId",
    "resource-path" : "$context.resourcePath"
    }
}

2 回答

  • 1

    您不必在模板中指定每个参数,您可以在映射模板中执行以下操作以传递任何参数:

    "queryParams": {
        #foreach($param in $input.params().querystring.keySet())
        "$param": "$util.escapeJavaScript($input.params().querystring.get($param))" #if($foreach.hasNext),#end
    
        #end
      }
    

    同样在API网关UI中,您现在只需在“生成模板”下拉列表中选择“方法请求直通”,它将为您创建一个模板,用于传递 Headers 和参数以及其他内容,而无需指定您想要的特定属性通过 .

  • 1

    AWS Forums中所述,您必须明确定义所有查询字符串参数 . 查询字符串参数没有直通支持 .

相关问题