Reputation: 1413
I have built an API in Flask that turns HTML pages through their URLS to PDF. However whenever the URL has slashes in it, the script outputs a 404.
this is the code -
def api(self):
app = Flask(__name__)
@app.route('/api/dodo/<filename>/<url>', methods=['GET', 'POST'])
def convert(url, filename):
content = request.json
outfile = pdfkit.from_url(str(url), filename)
return jsonify({"url":url, 'filename': filename})
app.run(debug=True)
if the request is localhost/api/dodo/filename/google.com
I do get the output file. But if I send this request
http://localhost/api/dodo/filename/https://dev.bizlem.io:8082/scorpio1/GALBRAITH'S_FUEL_27K-55K_ETA_CENTRAL_MED_LIST_-_14TH_JANUARY_2019_1.html
I get 404.
Upvotes: 0
Views: 81
Reputation: 663
From what I understand, this is happening because the url in the latter case actually maps to a different endpoint. Notice that the slashes added in the url change the meaning.
A better approach is to urlencode filename
and url
parameters, and pass them as GET parameters (From what I see, /api/dodo is your REST API endpoint and filename
, url
are parameters).
The code should look somewhat like this:
def api():
app = Flask(__name__)
@app.route('/api/dodo', methods=['GET'])
def convert():
url = request.args.get('url')
filename = request.args.get('filename')
outfile = pdfkit.from_url(url, filename)
return jsonify({"url":url, 'filename': filename})
app.run(debug=True)
The url to access the resource would then be:
http://127.0.0.1:5000/api/dodo?url=https%3A%2F%2Fdev.bizlem.io%3A8082%2Fscorpio1%2FGALBRAITH%27S_FUEL_27K-55K_ETA_CENTRAL_MED_LIST_-_14TH_JANUARY_2019_1.html&filename=okay
Upvotes: 1