'How to make a PUT request with a full URL/link as a path parameter using Flask?
I need to add a link to a photo in my database using REST API. I have tried to add it as a string, but there is some error.
My PUT call:
http://127.0.0.1:3000/addphoto/3&http://www.clker.com/cliparts/8/m/S/S/c/x/number-3-coner-num-hi.png
and what I get:
127.0.0.1 - - [09/Apr/2022 09:10:51] "PUT /addphoto/3%26http://www.clker.com/cliparts/8/m/S/S/c/x/number-3-coner-num-hi.png HTTP/1.1" 404
Here is the code for the PUT route:
@app.route('/addphoto/<int:id>&<string:link1>', methods=['PUT'])
def addphoto(id,link1):
connection = psycopg2.connect(db.data)
cursor = connection.cursor()
update_query = f"""UPDATE public."Items" SET photo = '{link1}' WHERE "Id" = {id}"""
cursor.execute(update_query)
connection.commit()
return getitems()
Solution 1:[1]
It seems you want to pass it through path parameters. I think it would lead to high chances of errors since the route path matching algorithm has a bit of a complicated rule.
I suggest to pass it through query params like this
from flask import request
@app.route('/addphoto', methods=['PUT'])
def addphoto():
id = request.args.get("id")
link1 = request.args.get("link")
connection = psycopg2.connect(db.data)
cursor = connection.cursor()
update_query = f"""UPDATE public."Items" SET photo = '{link1}' WHERE "Id" = {id}"""
cursor.execute(update_query)
connection.commit()
return getitems()
Then you can hit it, such as this example request:
http://127.0.0.1:5000/addphoto?id=200&link=https://www.youtube.com/watch?v=ei7kbQhK1hA
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Gino Mempin |