Geographic Information Systems Asked by nnolde on July 1, 2021
I have a custom Flask backend and a QGIS plugin as client. I make a request with the QgsNetworkAccessmanager
with blockingPost()
to an endpoint which returns a JSON response on either HTTP status.
That works fine for HTTP 200, but when an API error occurs, the content()
of the QgsNetworkReplyContent
is just empty (b''
). I’m fairly sure I’m missing smth on the Flask side, because in other plugins requesting other (non-Flask) APIs I can correctly see the content
error message.. I tried diving into the source code to see what the Flask response is missing, but it’s quite hard since the whole network module mixes Qt and Qgs components quite heavily.
EDIT: This is the (quite standard) error handler in Flask:
from werkzeug.exceptions import BadRequest
@api.errorhandler(BadRequest)
def handle_user_bad_post_error(e: BadRequest):
return {'error': e.description}, 400
EDIT2: forgot to say that all other requesting methods I tried (curl, Postman) did response with the correct error message
If you are able to catch the status of the HTTP code answer e.g different from 200, you can't get the error message from method errorString
and/or error
from your QgsNetworkReplyContent
object like illustrated below ?
I would try to change the URL from 2nd example with your own to see content of response.error()
and response.errorString()
Working URL, HTTP 200
workingUrl = QUrl('http://example.com')
request = QNetworkRequest(workingUrl)
networkAccessManager = QgsNetworkAccessManager()
response = networkAccessManager.blockingGet(request)
status_code = response.attribute(QNetworkRequest.HttpStatusCodeAttribute)
if status_code == 200:
print(response.content())
else:
print(response.error())
print(response.errorString())
"Unworking URL" e.g HTTP different than 200
unworkingUrl = QUrl('https://gis.stackexchange.com/error')
request = QNetworkRequest(unworkingUrl)
networkAccessManager = QgsNetworkAccessManager()
response = networkAccessManager.blockingGet(request)
status_code = response.attribute(QNetworkRequest.HttpStatusCodeAttribute)
if status_code == 200:
print(response.content())
else:
print(response.error())
print(response.errorString())
Answered by ThomasG77 on July 1, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP