question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Strange behavior when call is made from ajax, other domain

See original GitHub issue

Hi! I have a project where front-end(React) and back-end(Flask) are working on different domains.

I added the following test end-point:

@test _api.route('/test')
class test_route(Resource):
    @jwt_required
    def get(self):
        return jsonify({'result': True})

It’s working properly if I provide a valid access token (in header, as I can’t use it in the cookie).

But if it’s not provided, or it’s wrong I can’t make the following handler work right:

@jwt.unauthorized_loader
def unauthorized_loader_handler(message):
    return jsonify({
            "description": "Access token not found!",
            "error": "unauthorized_loader"
        }, 401)

in a case of unauthorized loader and get into this handler but right after the exception is thrown:

Server-side ERROR: ‘Response’ object has no attribute ‘get’

I just want it to return status 401 to the front-end.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
0xbartcommented, Oct 2, 2020

This is one of the options that should work with Flask-restx.

  1. Uncomment the private function _set_error_handler_callbacks(api).
  2. Add default handler for all JWTExtendedExtension

Example

import pathlib

from flask import Flask
from flask_restx import Api
from flask_jwt_extended import JWTManager
from flask_jwt_extended.exceptions import JWTExtendedException

def create_app():
    app = Flask(__name__)
    app.config.from_pyfile(pathlib.Path(__file__).parent / "config.py")

    api = Api(app)
    jwt = JWTManager()
    
    @api.errorhandler(JWTExtendedException)
    def handle_jwt_exceptions(error):
        return {'message': str(error)}, getattr(error, 'code', 401)

    jwt.init_app(app)
0reactions
adrianbeloquicommented, Nov 18, 2020

Thanks @0xbart !

While upgrading my app and moving from flask_restplus to flask_restx the option 1 (which was working with flask_restplus) was only working when running tox to test the app, but not when running the server for some reason.

The option 2 did the trick for me on both. Much appreciated!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Different behaviour in AJAX call in browser and "Copy as cURL"
This AJAX call shows a super strange behaviour: When initiated by the browser through the page's JavaScript, the call doesn't work as expected....
Read more >
Strange behavior with ajax requests (CORS problem) - General
This domain is behind Cloudflare and the web page simply make 2 POST Ajax calls to 2 differents subdomains. First one : https://demo1.rpgmax.fr/...
Read more >
Using CORS for Cross-Domain Ajax Requests
If the script on your page is running from domain mydomain.com and would like to request a resource via an XmlHttpRequest or XDomainRequst...
Read more >
My head hurts ~ DataTables with Cross Domain Ajax Request
I understand that json through AJAX over cross domain doesn't work without altering the code but, I am apparently missing something.
Read more >
Solutions to 5 Common Ajax Problems - Webdesigner Depot
This problem occurs when a designer has incorporated JavaScript and Ajax enhancements into their website's architecture without making ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found