Return a requests.Response object from Flask

前端 未结 5 1418
旧时难觅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 18:49

    Flask can return an object of type flask.wrappers.Response.

    You can create one of these from your requests.models.Response object r like this:

    from flask import Response
    
    return Response(
        response=r.reason,
        status=r.status_code,
        headers=dict(r.headers)
    )
    
    0 讨论(0)
  • 2020-12-02 18:52

    Ok, found it:

    If a tuple is returned the items in the tuple can provide extra information. Such tuples have to be in the form (response, status, headers). The status value will override the status code and headers can be a list or dictionary of additional header values.

    (Flask docs.)

    So

    return (resp.text, resp.status_code, resp.headers.items())
    

    seems to do the trick.

    0 讨论(0)
  • 2020-12-02 18:56

    Using text or content property of the Response object will not work if the server returns encoded data (such as content-encoding: gzip) and you return the headers unchanged. This happens because text and content have been decoded, so there will be a mismatch between the header-reported encoding and the actual encoding.

    According to the documentation:

    In the rare case that you’d like to get the raw socket response from the server, you can access r.raw. If you want to do this, make sure you set stream=True in your initial request.

    and

    Response.raw is a raw stream of bytes – it does not transform the response content.

    So, the following works for gzipped data too:

    esreq = requests.Request(method=request.method, url=url,
                             headers=request.headers, data=request.data)
    resp = requests.Session().send(esreq.prepare(), stream=True)
    return resp.raw.read(), resp.status_code, resp.headers.items()
    

    If you use a shortcut method such as get, it's just:

    resp = requests.get(url, stream=True)
    return resp.raw.read(), resp.status_code, resp.headers.items()
    
    0 讨论(0)
  • 2020-12-02 18:56

    I ran into the same scenario, except that in my case my requests.models.Response contained an attachment. This is how I got it to work:

    return send_file(BytesIO(result.content), mimetype=result.headers['Content-Type'], as_attachment=True)

    0 讨论(0)
  • 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()))
    
    0 讨论(0)
提交回复
热议问题