如何在不通过AWS API网关预先定义的情况下传递URL查询字符串参数
问题描述:
有没有在方法请求和集成请求中定义URL查询字符串参数并将查询字符串传递给HTTP代理的方法?如何在不通过AWS API网关预先定义的情况下传递URL查询字符串参数
例如,在HTTPS的情况下:// {API ID} -api.us-east-1.amazonaws.com/test/user?start=1 &端= 10
查询字符串 1 。开始 2.结束
如果我把方法请求和集成请求中的URL查询字符串参数,然后我能够得到两个查询字符串。但是,如果我不添加它们并在邮递员中发出请求,那么在后端,我没有收到两个查询字符串。
我想在没有在方法请求和集成请求中定义的情况下获取查询字符串。原因是如果有多个参数,我不想输入所有参数。
有没有办法做到这一点?
我正在使用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"
}
}
答
您不必指定模板中的每个参数,你可以做的映射模板像这样传递任何参数:
"queryParams": {
#foreach($param in $input.params().querystring.keySet())
"$param": "$util.escapeJavaScript($input.params().querystring.get($param))" #if($foreach.hasNext),#end
#end
}
而且API网关UI在现在您可以在“生成模板”下拉列表中选择“方法请求传递”,它将为您创建一个模板,传递标题和参数以及其他内容,而无需指定要传递的特定属性。
如您所说在集成请求中添加了模板,但它不起作用。如果我没有在URL查询字符串参数中指定,我仍然不会收到查询字符串。我没有使用Lambda函数。 – jgranda2
你可以添加你的问题映射模板? –
我刚刚添加了映射模板,它实际上就是您所说的下拉菜单中的一个。在后端,我没有得到请求中的任何查询字符串。 – jgranda2