title: Flask route to download .xml files leading to 404 Not Found
body: <p>I have a <code>flask</code> (0.10.1) app running on a <code>Debian Jessie VPS</code> and powered by <code>nginx</code> (1.6.2). The app is working fine but I have a problem on a specific <code>route</code> I added recently.</p>
  
  <p>The <code>route</code> is intended for downloading <code>.xml</code> files. </p>
  
  <p>It is dynamic to tell the directory and the file name:</p>
  
  <pre><code>@app.route('/backups/&lt;dir_key&gt;/&lt;filename&gt;')
  </code></pre>
  
  <p>And it registers a function based on the <code>flask</code> <code>send_from_directory</code> function:</p>
  
  <pre><code>def backups(dir_key,filename):
      directory = os.path.join(app.config['BACKUPXML_FOLDER'], dir_key)
      return send_from_directory(directory, filename, as_attachment=True)
  </code></pre>
  
  <p>The route is generated thanks to the <code>flask</code> <code>url_for</code> function, and returned to the frontend:</p>
  
  <pre><code>return jsonify({
      'backupFileUrl': url_for('backups', dir_key=dir_key, filename = filename, _external=True)
  })
  </code></pre>
  
  <p>where it is stored in an <code>AngularJS</code> variable:</p>
  
  <pre><code>$scope.backupFileUrl = response.backupFileUrl;
  </code></pre>
  
  <p>And finally included in a <code>&lt;a&gt;</code> tag for download :</p>
  
  <pre><code>&lt;a class="btn btn-primary"
     ng-show="sessionDownload"
     ng-href="{{ backupFileUrl }}" target="_blank"&gt;
     &lt;span class="glyphicon glyphicon-save"&gt;&lt;/span&gt; Télécharger &lt;/a&gt;
  </code></pre>
  
  <p>But when I click on the button, I get the following error :</p>
  
  <p><a href="https://i.stack.imgur.com/SNHYu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SNHYu.png" alt="enter image description here"></a></p>
  
  <p>What is weird to is that :</p>
  
  <ol>
  <li>The download is properly triggered when the app is powered by a small <code>Python</code> server on a local <code>Windows</code> machine.</li>
  <li>I have a <code>route</code> intended for downloads of <code>.xlsx</code> files which is actually working, and both on a local <code>Windows</code> machine and on the <code>Jessie VPS</code>.</li>
  </ol>
  
  <p>Someone see how I can define the <code>route</code> to make it work ?</p>
  
  <p>Here is the api architecture if needed :</p>
  
  <p><strong>api/app.py</strong> </p>
  
  <pre><code>import sys
  sys.path.append('../')
  
  from flask_script import Server, Manager
  from kosapp import app, db
  
  manager = Manager(app)
  
  if __name__ == '__main__':    
      manager.run()
  </code></pre>
  
  <p><strong>api/config.py</strong></p>
  
  <pre><code>from os.path import abspath, dirname, join
  import tempfile
  
  basedir = dirname(abspath(__file__))
  BASEDIR = dirname(abspath(__file__))
  
  DEBUG = True
  
  REPORTS_FOLDER = '/tmp/reports'
  # on local machine 
  # REPORTS_FOLDER = os.path.join(tempfile.gettempdir(), 'reports') 
  
  BACKUPXML_FOLDER = '/tmp/backups'
  # on local machine
  # BACKUPXML_FOLDER = os.path.join(tempfile.gettempdir(), 'backups')
  </code></pre>
  
  <p><strong>api/kosapp/__init__.py</strong> </p>
  
  <pre><code>from flask import Flask
  
  app = Flask(__name__)
  app.url_map.strict_slashes = False
  app.config.from_object('config')
  
  from kosapp import views
  </code></pre>
  
  <p><strong>api/kosapp/views.py</strong></p>
  
  <pre><code>import os
  
  from flask import send_file, jsonify, request, render_template, send_from_directory
  
  from kosapp import app
  
  @app.route('/reports/&lt;dir_key&gt;/&lt;filename&gt;')
  def reports(dir_key, filename):
      directory = os.path.join(app.config['REPORTS_FOLDER'], dir_key)
      return send_from_directory(directory, filename)
  
  @app.route('/backups/&lt;dir_key&gt;/&lt;filename&gt;')
  def backups(dir_key,filename):
      directory = os.path.join(app.config['BACKUPXML_FOLDER'], dir_key)
      return send_from_directory(directory, filename, as_attachment=True)
  </code></pre>
  
  <p>As a note, the route <code>'/reports/&lt;dir_key&gt;/&lt;filename&gt;'</code> is intended for downloading <code>.xlsx</code> file and works fine.</p>