Replies: 1 comment
-
Hi! @radder5 Even if you simply want to respond with For example, you can forcibly cast it to a string like this, but I think it's difficult to use it in a general way. @app.get("/hello")
def hello() -> str:
return '{"message": "Hello, world!"}' # string Therefore, I believe the focus of the problem should be adjusted to parsing the JSON on the client side rather than on the server side. For example, if you are using Node.js, you can use fetch to convert the response to JSON like this: const fetch = require('node-fetch');
async function getTimings() {
const response = await fetch('http://your-api-endpoint');
const data = await response.json();
console.log(data);
}
getTimings(); Finally, this is another option, "customizing the integration response in API Gateway" is also a possible solution. |
Beta Was this translation helpful? Give feedback.
-
using the examples from https://docs.powertools.aws.dev/lambda/python/latest/core/event_handler/api_gateway/#lambda-function-url for a dict return value from a route, I'd like to keep that as a nested object within the body property but this always gets serialized to a JSON string.
e.g.
@app.get("/timings/<year>/<month>") def get_timings_by_month_year(year: Annotated[int, Path(ge=2023,le=datetime.now().year)], month: Annotated[int, Path(le=12, ge=1)]) -> dict: return {"timings": "some timings"}
returns
{ "statusCode": 200, "body": "{\"timings\":\"some timings\"}", "isBase64Encoded": false, "headers": { "Content-Type": "application/json" }, "cookies": [] }
I would like it to return
{ "statusCode": 200, "body": { "timings": "some timings" }, "isBase64Encoded": false, "headers": { "Content-Type": "application/json" }, "cookies": [] }
is this possible?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions