What I’m trying to do:
I’m trying to use anvil as an API backend, I created an API endpoint with some errors in the code, but after I fixed the code, when I try to call the API, it’s still calling the old one.
Is there any way to restart the anvil app server? Or clear the api “cache”?
Btw, my API endpoint name is /tasks/3d-image/upload-image, if I try to rename it to upload-image-2, or anything relate, doesn’t work, if i rename it to /test-2 (I already have one named test) then it works correctly.
What I’ve tried and what’s not working:
Tried republishing the app with different link, changing branches, changing the API endpoint name, it always calls the old one for some reason.
Code Sample:
@anvil.server.http_endpoint("/test-2", methods=["POST"])
def test_api_2():
data = anvil.server.request.body_json
try:
row_id = data["row_id"]
image_base64 = data["image_base64"]
row = app_tables.rendered_images.get(image_id=row_id)
if row:
row.update(image=image_base64)
return {"status": "success", "message": "Preview uploaded successfully"}
else:
return {
"status": "error",
"message": f"Image Task with ID {row_id} not found",
}
except Exception as e:
return anvil.server.HttpResponse(500, f"Internal server error: {str(e)}")
Since I have renamed my http-endpoint, and you can see I can still call it from postman. (I have all http-endpoints in one file, you can see I am searching for that endpoint and I can’t find it
I have since updated the function to return the id of the task it’s not finding, previous version of the endpoint only returned the message “Task not found”
After going through and refactoring the code, I have found my error:
I have one endpoint called “creation-module/ai-image/:row_id” , and another one after that “/creation-module/3d-image/upload-preview”, so when I was trying to call the second one, it was actually trying to call the first one, with the row_id = "upload-preview.
Is this a bug or am I naming my API endpoints wrong? One of them is a post, another is a get, I thought it won’t cause any conflicts.
I’m guessing it might be a bug, since I moved the GET blocks of code at the end of the module, the error is gone, and I can successfully call all APIs now.