Reputation: 1031
I have written the below code and have deployed in AWS Lambda using deployment package :
from flask import Flask,jsonify,request
app = Flask(__name__)
books = [
{'id': 0,
'title': 'A Fire Upon the Deep',
'author': 'Vernor Vinge',
'first_sentence': 'The coldsleep itself was dreamless.',
'year_published': '1992'},
{'id': 1,
'title': 'The Ones Who Walk Away From Omelas',
'author': 'Ursula K. Le Guin',
'first_sentence': 'With a clamor of bells that set the swallows soaring, the Festival of Summer came to the city Omelas, bright-towered by the sea.',
'published': '1973'},
{'id': 2,
'title': 'Dhalgren',
'author': 'Samuel R. Delany',
'first_sentence': 'to wound the autumnal city.',
'published': '1975'}
]
@app.route("/listbooks",methods=['GET'])
def hello():
return jsonify(books)
@app.route("/getbookbyid",methods=['GET'])
def getBybookid(event,context):
if 'id' in request.args:
id=int(request.args['id'])
results=[]
for book in books:
if book['id']==id:
results.append(book)
return jsonify(results)
I have configured API Gateway to hit the lambda function for a GET request. When i am trying to hit the endpoint using postman I am getting :
Working outside of request context error
Any pointers
Upvotes: 0
Views: 659
Reputation: 39
Please add the below line. It will solve your error.
app.app_context().push()
-Prasanna.K
Upvotes: 0
Reputation: 78883
The error message "Working outside of request context" is a Flask error, not an API Gateway or Lambda error. It happens when your Flask app tries to access request
, or anything that uses it, outside of a request context.
As @BSQL suggests, this seems to be because of the incorrect indentation of your code.
Upvotes: 1