Skip to content

Instantly share code, notes, and snippets.

@yejianye
Created November 15, 2013 09:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yejianye/7481508 to your computer and use it in GitHub Desktop.
Save yejianye/7481508 to your computer and use it in GitHub Desktop.
Set mime type of response object in Flask
@app.route('/download', methods=['GET'])
def download():
data = json.dumps({"example" : "json string"})
response = make_response(data)
response.headers['Content-Type'] = 'text/json'
response.headers['Content-Disposition'] = 'attachment; filename=example.json'
return response
@aakashkaji
Copy link

how could i send token in header in flask restful api

@ecarrera
Copy link

ecarrera commented Aug 8, 2019

If all your responses will have only one response content-type this approach is pretty useful (implemented on the flask app declaration):

@app.after_request
def after_request_func(data):
    response = make_response(data)
    response.headers['Content-Type'] = 'application/json'
    return response

'application/json' as my default response type for a basic API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment