Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

aws api gateway - How to pass query parameters to ApiGateway v2 HttpApi through Cloudfront Distribution?

I am using aws-cdk for TypeScript. I have an ApiGatewayv2 HttpApi with the following config:

new HttpApi(this, "Api", {
            defaultIntegration: new LambdaProxyIntegration({
                handler: new NodejsFunction(this, "Handler", {
                    handler: "handler",
                    entry: `${__dirname}/api-default.handler.ts`
                }),
                payloadFormatVersion: PayloadFormatVersion.VERSION_2_0
            }),
            createDefaultStage: true,
            corsPreflight: {
                allowHeaders: ['Authorization'],
                allowMethods: [HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS, HttpMethod.POST],
                allowOrigins: ['*'],
                maxAge: Duration.days(10),
            }
        })

When I make a request to the API passing query parameters (using Postman) I see them in the handler payload attributes queryStringParameters and rawQueryString.

E.g. I make a GET request to https://MY_API_HASH.execute-api.eu-west-1.amazonaws.com?param=foo

the event, received in the Lambda handler looks like:

{
    ...
    "rawQueryString": "param=foo",
    ...
    "queryStringParameters": {
        "param": "foo"
    },
    ...
}

I made a Cloudfront Distribution with the following config:

new Distribution(this, "Distribution", {
            defaultBehavior: {
                origin: new S3Origin(bucket)
            },
            additionalBehaviors: {
                "api": {
                    origin: new HttpOrigin(api.url!.split("/")[2]),
                    originRequestPolicy: new OriginRequestPolicy(this, "ApiOriginRequestPolicy", {
                        headerBehavior: OriginRequestHeaderBehavior.all(),
                        queryStringBehavior: OriginRequestQueryStringBehavior.all(),
                    }),
                    allowedMethods: AllowedMethods.ALLOW_ALL,
                    viewerProtocolPolicy: ViewerProtocolPolicy.ALLOW_ALL
                }
            }
        })

When I make a request to the API through the Distribution URL I don't see in the payload "rawQueryString": "" and no queryStringParameters


How can I pass query parameters to the API handler when requesting the Cloudfront Distribution URL?

question from:https://stackoverflow.com/questions/65952273/how-to-pass-query-parameters-to-apigateway-v2-httpapi-through-cloudfront-distrib

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...