[Py4web 1.20210906.1 / Ombott] Request context not initialized.

95 views
Skip to first unread message

Mamisoa Andriantafika

unread,
Nov 1, 2021, 9:33:48 AM11/1/21
to py4web
Hi,

I upgraded to the latest py4web version 1.20210906.1 with ombott.
While the app was working with the previous version with bottle, I had some issues.

One of them is to handle the header responses. I try to get a specific JSON output and some CORS headers. Here is a sample controller:

@action('api/beid', method=['GET'])
def beid():
import json, bottle
from base64 import b64encode
from .beid import scan_readers, read_infos, triggered_decorator
from time import sleep
r = scan_readers()[0]
infos_json = {}
response = bottle.response
response.headers['Content-Type'] = 'application/json;charset=UTF-8'
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS'
response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
try:
# sleep(2)
infos = read_infos(r, read_photo=True)
infos['photo'] = b64encode(infos['photo']).decode('utf8')
except Exception as e:
infos = { 'results': 'cannot read card', 'erreur': e.args}
infos_json = json.dumps(infos)
return infos_json

I now get the following exception:

Traceback (most recent call last): File "/home/mamisoa16/code/py4web/lib/python3.8/site-packages/py4web/core.py", line 901, in wrapper ret = func(*func_args, **func_kwargs) File "/home/mamisoa16/code/py4web/apps/myapp/rest.py", line 60, in beid response.headers['Content-Type'] = 'application/json;charset=UTF-8' File "/home/mamisoa16/code/py4web/bin/bottle.py", line 1537, in headers hdict.dict = self._headers File "/home/mamisoa16/code/py4web/bin/bottle.py", line 1671, in fget raise RuntimeError("Request context not initialized.") RuntimeError: Request context not initialized.

What is odd is that sometimes I do GET the request, but without the headers.

Any ideas?

Mamisoa

Mamisoa Andriantafika

unread,
Nov 1, 2021, 11:04:18 AM11/1/21
to py4web
Ok I got it working by removing the bottle import...
Reply all
Reply to author
Forward
0 new messages