简体   繁体   中英

Remove Headers from Flask Response

I am trying to develop a web service back-end for an Alexa skill, and this requires me to have very specific headers in the HTTP response.

Looking at the details of my response (using hurl.it), I have a whole bunch of HTTP headers that Amazon doesn't want. How can I remove the 'X-Clacks-Overhead', the 'Server', etc., responses.

I am using Flask and Python 3.

You can't, but I seriously doubt that anyone would write code that would fall down when there were extra headers fields in a request. Perhaps you're misinterpreting the error.

If your header is actually being set by Flask, you can remove it from the headers dict on the response by using Flask's after_request function/decorator:

@app.after_request
def remove_header(response):
    del response.headers['X-Some-Custom-Header']

    return response

With the likes of Server , it's likely this is being set by an upstream provider and not Flask directly, so you'd need to remove it from whatever is proxying the request from Flask and outputting to the user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM