英文:
Flask Application on Azure App Service throwing "405 Method Not Allowed" error
问题
我有一个简单的Flask应用程序,如下所示,它使用Flask Blueprint功能进行模块化。在本地部署和测试Flask应用程序时,一切正常运行,我可以看到预期的输出,但当应用程序部署到Azure应用服务并且从Postman或Python发送请求时,我收到以下错误。有人可以告诉我我漏掉了什么。
<!doctype html>
<html lang=en>
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1>
<p>The method is not allowed for the requested URL.</p>
以下是相关数据
应用程序结构
/root
|- app.py
|- routes
| |- test.py
|- send_request.py
|- requirements.txt
test.py
from flask import Blueprint, request, jsonify
route = Blueprint("test", __name__, url_prefix="/test")
@route.route("/", methods=["POST"])
def test():
data = request.json["test"]
print(data)
return jsonify(data)
app.py
from flask import Flask
from routes import test
app = Flask(__name__)
app.register_blueprint(test.route)
if __name__ == "__main__":
app.run(debug=True)
send_request.py
import requests
import json
url = "https://<app>.azurewebsites.net/test"
payload = json.dumps({
"test": "Hello World!"
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
英文:
I have a simple Flask app as shown below which is modularized using Flask Blueprint functionality. On local deployment & testing Flask App is running without any issue, I can see the expected output but when the app is deployed on the Azure App service and the request is sent from either Postman or Python then I am getting the below error. Can anyone tell me what I am missing.
<!doctype html>
<html lang=en>
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1>
<p>The method is not allowed for the requested URL.</p>
Below are relevant data
App structure
/root
|- app.py
|- routes
| |- test.py
|- send_request.py
|- requirements.txt
test.py
from flask import Blueprint, request, jsonify
route = Blueprint("test", __name__, url_prefix="/test")
@route.route("/", methods=["POST"])
def test():
data = request.json["test"]
print(data)
return jsonify(data)
app.py
from flask import Flask
from routes import test
app = Flask(__name__)
app.register_blueprint(test.route)
if __name__ == "__main__":
app.run(debug=True)
send_request.py
import requests
import json
url = "https://<app>.azurewebsites.net/test"
payload = json.dumps({
"test": "Hello World!"
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
答案1
得分: 1
通常情况下,这些错误通常发生在URL未正确提供或服务器不支持请求中使用的HTTP方法时。这意味着请求中使用的方法不允许用于您请求的URL。
考虑到您当前的问题,Web应用程序的URL是不正确的,正如@PGHE所指出的那样。因为
https://webapp.azurewebsites.net/xxxx
≠ https://webapp.azurewebsites.net/xxxx/
。
在除根域之后的所有情况下,尾随斜杠将被视为单独的URL。而响应和请求正文看起来都很好。
在门户中,Web应用程序部署的URL将采用以下格式显示。但是,在从本地部署时,我们必须在应用程序域URL的末尾包括"/"
。
我的requirements.txt:
requests==2.18.4
flask==2.3.2
routes==2.5.1
我修改了sendrequest.py如下:
import requests
import json
url = "https://<mywebapp>.azurewebsites.net/test/"
payload = json.dumps({
"test": "Hello"
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
成功调试后,Flask应用程序在以下服务器HTTP服务器上运行。
这是对您提供的内容的翻译,不包含代码部分。
英文:
Usually, these errors occur either when the URL is not correctly given or when the server does not support the HTTP method used in the request. It means that, the method used in the request isn't permitted for the URL you've requested.
If we consider the current issue of yours, the web app URL is incorrect as pointed out by @PGHE. Because
https://webapp.azurewebsites.net/xxxx
≠ https://webapp.azurewebsites.net/xxxx/
.
A trailing slash will be handled as a separate URL in all cases except the one just after the root domain. And the response, request body looks good.
In the portal, the web app deployments URL will be in the below shown format. However, while deploying from local, we must include "/"
at the end of the app domain URL.
My requirements.txt:
requests==2.18.4
flask==2.3.2
routes==2.5.1
I modified sendrequest.py as below:
import requests
import json
url = "https://<mywebapp>.azurewebsites.net/test/"
payload = json.dumps({
"test": "Hello"
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
After debugging successfully, flask app is running on the below server http server.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论