Flask Application on Azure App Service throwing "405 Method Not Allowed" error

huangapple go评论66阅读模式
英文:

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.

&lt;!doctype html&gt;
&lt;html lang=en&gt;
&lt;title&gt;405 Method Not Allowed&lt;/title&gt;
&lt;h1&gt;Method Not Allowed&lt;/h1&gt;
&lt;p&gt;The method is not allowed for the requested URL.&lt;/p&gt;

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(&quot;test&quot;, __name__, url_prefix=&quot;/test&quot;)

@route.route(&quot;/&quot;, methods=[&quot;POST&quot;])
def test():
   data = request.json[&quot;test&quot;]
   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__ == &quot;__main__&quot;:
   app.run(debug=True)

send_request.py

import requests
import json

url = &quot;https://&lt;app&gt;.azurewebsites.net/test&quot;

payload = json.dumps({
  &quot;test&quot;: &quot;Hello World!&quot;
})
headers = {
  &#39;Content-Type&#39;: &#39;application/json&#39;
}

response = requests.request(&quot;POST&quot;, url, headers=headers, data=payload)
print(response.text)

答案1

得分: 1

通常情况下,这些错误通常发生在URL未正确提供或服务器不支持请求中使用的HTTP方法时。这意味着请求中使用的方法不允许用于您请求的URL。

考虑到您当前的问题,Web应用程序的URL是不正确的,正如@PGHE所指出的那样。因为

https://webapp.azurewebsites.net/xxxxhttps://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/xxxxhttps://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 &quot;/&quot; at the end of the app domain URL.

Flask Application on Azure App Service throwing "405 Method Not Allowed" error

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 = &quot;https://&lt;mywebapp&gt;.azurewebsites.net/test/&quot;
payload = json.dumps({
&quot;test&quot;: &quot;Hello&quot;
 })
headers = {
&#39;Content-Type&#39;: &#39;application/json&#39;
}
response = requests.request(&quot;POST&quot;, url, headers=headers, data=payload)
print(response.text)

Flask Application on Azure App Service throwing "405 Method Not Allowed" error

After debugging successfully, flask app is running on the below server http server.

Flask Application on Azure App Service throwing "405 Method Not Allowed" error

Flask Application on Azure App Service throwing "405 Method Not Allowed" error

huangapple
  • 本文由 发表于 2023年6月1日 01:34:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定