The problem he was having in the video (I think) was that because he stored the queryStringParameters into x and y and then directly passed them into add(x, y) but they were strings. The code here in the comments converts them to int before passing them to add, but I would convert them right away as soon as you assign them to x and y because at no point in lambda_handler you need them to be strings and that saves from converting them 2 times
An even better bug fix for my lambda function would have been to change lines 4 and 5 to int(event[…]) for x and y, or float() if I wanted my calculator to handle decimal numbers.
This was very well explained. What I'm not getting, is what are all these specific json keys in the response that you are constructing? Where are these defined? 'statusCode', 'headers', etc? What if I just wanted to return a random hard coded JSON, why can't the lambda work so: import json def lambda_handler(event, context): print(event) return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!'), 'SudentID': '100' 'StudentName' : ''Vincent' 'Age': '25' } Thanks
when i try to deploy the api and access the link to display the result its throwing a message of "Missing Authentication Token" any idea what's causing this and how to solve it?
Beautifully explained. Thank you. ❤
For the viewers-
import json
def lambda_handler(event, context):
x = event['queryStringParameters']['x']
y = event['queryStringParameters']['y']
op = event['queryStringParameters']['op']
print(f"x:{x}, y:{y}, op:{op}")
res_body = {}
res_body['x'] = int(x)
res_body['y'] = int(y)
res_body['op'] = op
res_body['ans'] = add(x,y)
http_res = {}
http_res['statusCode'] = 200
http_res['headers'] = {}
http_res['headers']['Content-Type'] = 'application/json'
http_res['body'] = json.dumps(res_body)
return http_res
def add(x,y):
return x+y
?x=2&y=10&op=add
For those who want code he used:
import json
def lambda_handler(event, context):
x=event['queryStringParameters']['x']
y=event['queryStringParameters']['y']
op=event['queryStringParameters']['op']
#Log inputs
print(f"x:{x}, y: {y}, op: {op}")
#prepare the response_body
res_body = {}
res_body['x'] = int(x)
res_body['y'] = int(y)
res_body['op'] = op
res_body['ans'] = add(int(x),int(y))
#prepare http response
http_res = {}
http_res['statusCode'] = 200
http_res['headers'] = {}
http_res['headers']['Content-Type'] = 'application/json'
http_res['body'] = json.dumps(res_body)
return http_res
def add(x,y):
return x+y
The problem he was having in the video (I think) was that because he stored the queryStringParameters into x and y and then directly passed them into add(x, y) but they were strings. The code here in the comments converts them to int before passing them to add, but I would convert them right away as soon as you assign them to x and y because at no point in lambda_handler you need them to be strings and that saves from converting them 2 times
Thank you this was one of the only tutorials that worked for me. I'm an swe intern at amazon btw so thanks again
Use "parseInt()" function instead of "int()". Thanks for the tutorial.
Thank you so much. This helped me a lot.
An even better bug fix for my lambda function would have been to change lines 4 and 5 to int(event[…]) for x and y, or float() if I wanted my calculator to handle decimal numbers.
This was very well explained. What I'm not getting, is what are all these specific json keys in the response that you are constructing? Where are these defined?
'statusCode', 'headers', etc?
What if I just wanted to return a random hard coded JSON, why can't the lambda work so:
import json
def lambda_handler(event, context):
print(event)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!'),
'SudentID': '100'
'StudentName' : ''Vincent'
'Age': '25'
}
Thanks
that was fun, thank you, Vincent.
Awesome explanation. To the point. Thanks.
when i try to deploy the api and access the link to display the result its throwing a message of "Missing Authentication Token" any idea what's causing this and how to solve it?
Thanks for the great demo!
Thanks nice little intro.
Great video! Looks like queryStringParameters doesnt work in AWS with the new Python 3.10. Any ideas how to correct this?
hi i want to change path and send query string by javascript so i can filter my data. how can i do it?
Great video, well explained, thanks
{
"message": "Internal server error"
}
can you please do this using javascript without manual
I mostly agree!
Thank you!
Thanks
thanks!