Return a requests.Response object from Flask

前端 未结 5 1440
旧时难觅i
旧时难觅i 2020-12-02 18:10

I\'m trying to build a simple proxy using Flask and requests. The code is as follows:

@app.route(\'/es///

        
5条回答
  •  渐次进展
    2020-12-02 19:08

    My use case is to call another API in my own Flask API. I'm just propagating unsuccessful requests.get calls through my Flask response. Here's my successful approach:

    headers = {
        'Authorization': 'Bearer Muh Token'
    }
    try:
        response = requests.get(
            '{domain}/users/{id}'\
                .format(domain=USERS_API_URL, id=hit['id']),
            headers=headers)
        response.raise_for_status()
    except HTTPError as err:
        logging.error(err)
        flask.abort(flask.Response(response=response.content, status=response.status_code, headers=response.headers.items()))
    

提交回复
热议问题