errors when connecting a FastAPI lambda from API Gateway

0

Hello,

I have this fast API service which I'm testing. For "/users" endpoint it works fine but when I try to pass parameters I get errors. I have defined GET methods as

`@app.get("/users") async def get_users(): res = {"message": "Hello from Users!"} print(res) return res

@app.get("/employees/id") async def create_user(id: str): res = {"employee_id": id} print(res) return res ` Here is how the request is mapped

#set($allParams = $input.params()) { "version": "2.0", "routeKey": "$default", "queryParams": { "id" : "$input.params('id')" }, "requestContext": { "accountId": "$context.identity.accountId", "apiId": "$context.apiId", "domainName": "$context.apiId", "domainPrefix": "id", "http": { "method": "$context.httpMethod", "path": "/$allParams.path.proxy", "protocol": "HTTP/1.1", "sourceIp": "192.168.0.1/32", "userAgent": "agent" }, "requestId": "id", "routeKey": "$default", "stage": "$default", "time": "12/Mar/2020:19:03:58 +0000", "timeEpoch": 1583348638390 }, "body": "asdf" } But I get this error

{"statusCode": 422, "body": "{"detail":[{"loc":["query","id"],"msg":"field required","type":"value_error.missing"}]}", "headers": {"content-length": "87", "content-type": "application/json"}, "isBase64Encoded": false}

This is how the request is transformed

Endpoint request body after transformations: { "version": "2.0", "routeKey": "", "queryParams": { "id" : "jehan" }, "requestContext": { "accountId": "357209931375", "apiId": "pb79yrfxf7", "domainName": "pb79yrfxf7", "domainPrefix": "id", "http": { "method": "GET", "path": "/employees/id", "protocol": "HTTP/1.1", "sourceIp": "192.168.0.1/32", "userAgent": "agent" }, "requestId": "id", "routeKey": "", "stage": "", "time": "12/Mar/2020:19:03:58 +0000", "timeEpoch": 1583348638390 }, "body": "asdf" }

What I'm doing wrong here. is it correct how the parameters are passed?

additional info: this works fine

@app.get("/employees/{id}") async def create_user(id: str): res = {"employee_id": id} print(res) return res

1 Answer
0
Accepted Answer

I found out how to pass the parameters to lambda REST API. should be passed like this "rawQueryString": "parameter1=value1&parameter2=value2"

Jehan
answered 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions