FLASK中的200成功代码之前出现302重定向错误 [英] 302 redirection error before 200 success code IN FLASK

查看:552
本文介绍了FLASK中的200成功代码之前出现302重定向错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行FLASK应用程序,在其中检查索引端点中的JWT.问题是执行此端点时得到2个响应.

I am running an FLASK app where I check the JWT in the index endpoint. The problem was I get 2 response when I execute this end point .

127.0.0.1 - - [06/Dec/2018 17:38:21] "GET / HTTP/1.1" 302 -
127.0.0.1 - - [06/Dec/2018 17:38:21] "GET /home HTTP/1.0" 200 - 

我的代码是

@app.route('/')
def index():
    try:
        encoded_jwt=request.headers.get('jwt')
        print(encoded_jwt)
        secret = "-----BEGIN PUBLIC KEY----- ........"

        claims = jwt.decode(encoded_jwt, secret)
        print(type(claims))
        import json

        json_parse = json.loads(json.dumps(claims))
        email = json_parse['http://wso2.org/claims/emailaddress']
        print(email)
        roles = json_parse['http://wso2.org/claims/role']
        print(roles)

        session['email'] = email

        if ROLETAGOFADMIN in roles:
            role="admin"

        elif "" in roles:
            role = "employee"

        else:
            role=None

        session['role'] = role

        if 'email' in session and (session['role'] == "admin" or session['role'] == "employee"  )and request.method == "GET":
            if 'lastpage' in session:
                lastpage=session['lastpage']
                print(lastpage)
                session.pop('lastpage')
                return  redirect(lastpage)
            else:
                return redirect(url_for('home'))
        else:
            return "Sorry. Unfortunately You have no access."

    except Exception as e:
        return redirect(url_for('error'))

由于第一个响应,我的身份服务器正在重定向.我找不到解决该问题的方法.我不知道由于try catch发生了错误.请帮我.

My Identity server is redirecting because of the first response. I couldn't find the way to fix that. I am not aware of that the error is occurred because of the try catch . Please help me.

推荐答案

您正在使用Flask的

You are using Flask's redirect to issue a redirect which is going to send a 302 response to the client with a Location header instructing the client to go to /home instead. Then the client has to issue the request to this new URL where the client finally gets the 200 response code. That is why you are seeing two requests and the 302 and 200 response codes in the server logs.

此特定行引起了重定向:

This particular line is causing the redirect:

return redirect(url_for('home'))

您似乎希望redirect只是呈现/home的内容,并将其作为对原始请求发送给/的响应(例如,单个200响应)返回.如果那是您真正想要的,则可以改用 render_template (或您在/home中使用的用于呈现内容的任何内容)直接呈现该页面.但是,我建议您保持重定向行为不变.

It seems like you expected redirect to simply render the content of /home and return that as the response with the original request to / (e.g. a single 200 response). If that's what you actually want, you could instead use render_template (or whatever you use in /home to render your content) to directly render that page. However, I would recommend keeping the redirect behavior as you have it.

这篇关于FLASK中的200成功代码之前出现302重定向错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆