python - FLASK: Serving file to browser behind API proxy -
when user enters http://example2.com:5500/?param=x
code below generates data.csv file , serves browser. works this.
however, have deployed behind api proxy, user makes call http://example1.com/?param=x
internally transformed http://example2.com:5500/?param=x
.
as result, instead of serving data.csv browser before, displays on browser data.csv content. view source-code feature shows data.csv should contain, without html headers, data.csv content, not being served attachement. ideas?
from flask import make_response @app.route('/', methods = ['get']) def get_file(): alldata = [] while len(new_data) > 0: new_data = api.timeline(max_id=oldest) alldata.extend(new_data) oldest = alldata[-1].id - 1 outdata = "" data in alldata: outdata += ",".join(data) + "\n" response = make_response(outdata) response.headers["content-disposition"] = "attachment; filename=data.csv" return response if __name__ == '__main__': app.run(host = app.config['host'], port = app.config['port'])
edit: included mapping code transform request example1.com example2.com (secret_url)
# example1.com @app.route("/api/<projecttitle>/<path:urlsuffix>", methods=['get']) def projecttitlepage(projecttitle, urlsuffix): projectid = databasefunctions.gettitleprojectid(projecttitle) projectinfo = databasefunctions.getprojectinfo(projectid) redirectionquerystring = re.sub('apikey=[^&]+&?', '', request.query_string).rstrip('&') redirectionurl = projectinfo['secreturl'].rstrip('/') if urlsuffix not none: redirectionurl += '/' + urlsuffix.rstrip('/') redirectionurl += '/?' + redirectionquerystring redirectionheaders = request.headers print request.args.to_dict(flat=false) try: r = requests.get(redirectionurl, data=request.args.to_dict(flat=false), headers=redirectionheaders) except exception, e: return '/error=error: bad secret url: ' + projectinfo.get('secreturl') return r.text
your homegrown proxy not returning headers application. try this:
@app.route("/api/<projecttitle>/<path:urlsuffix>", methods=['get']) def projecttitlepage(projecttitle, urlsuffix): # ... return r.text, r.status_code, r.headers
Comments
Post a Comment