FastAPI 拒绝来自 javascript 代码的 POST 请求,但不拒绝来自第 3 方请求应用程序的 POST 请求(失眠)
当我使用 insomnia 发送帖子请求时得到 200 代码,一切工作正常,但是当我通过 javascript 发送 fetch 请求时,我收到 405“方法不允许错误”,即使我已经允许来自服务器端的 post 请求。 (服务器端代码使用python)。
服务器端代码
from pydantic import BaseModel
from typing import Optional
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["POST", "GET"],
allow_headers=["*"],
)
class data_format(BaseModel):
comment_id : int
username : str
comment_body : Optional[str] = None
@app.post('/post/submit_post')
async def sumbit_post(somename_3: data_format):
comment_id = somename_3.comment_id
username = somename_3.username
comment_body = somename_3.comment_body
# add_table_data(comment_id, username, comment_body) //Unrelated code
return {
'Response': 'Submission received',
'Data' : somename_3
}
JS代码
var payload = {
"comment_id" : 4,
"username" : "user4",
"comment_body": "comment_4"
};
fetch("/post/submit_post",
{
method: "POST",
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json'
}
})
.then(function(res){ return res.json(); })
.then(function(data){ alert( JSON.stringify( data ) ) })
错误
我应该怎么做才能解决这个问题错误?
提前致谢。
When I use insomnia to send a post request I get a 200 code and everything works just fine, but when I send a fetch request through javascript, I get a 405 'method not allowed error', even though I've allowed post requests from the server side.
(Server side code uses python).
Server side code
from pydantic import BaseModel
from typing import Optional
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["POST", "GET"],
allow_headers=["*"],
)
class data_format(BaseModel):
comment_id : int
username : str
comment_body : Optional[str] = None
@app.post('/post/submit_post')
async def sumbit_post(somename_3: data_format):
comment_id = somename_3.comment_id
username = somename_3.username
comment_body = somename_3.comment_body
# add_table_data(comment_id, username, comment_body) //Unrelated code
return {
'Response': 'Submission received',
'Data' : somename_3
}
JS code
var payload = {
"comment_id" : 4,
"username" : "user4",
"comment_body": "comment_4"
};
fetch("/post/submit_post",
{
method: "POST",
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json'
}
})
.then(function(res){ return res.json(); })
.then(function(data){ alert( JSON.stringify( data ) ) })
The error
What should I do to get around this error?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您的代码似乎工作得很好。在测试过程中(本地)唯一需要更改的部分是 fetch 中的 URL,从
/post/submit_post
到(例如)http://127.0.0.1:8000/post /submit_post
,但我假设您已经使用指向您的应用程序的域名进行了更改。不允许使用 405 方法
状态码与 CORS 无关。如果allow_methods
列表中未包含POST
,则响应状态代码将为400 Bad Request
(您可以尝试将其从列表中删除来测试)。从上面的参考资料来看:因此,405 状态代码表示服务器已接收并识别
POST
请求,但服务器已拒绝该特定 该特定端点的 HTTP 方法。因此,我建议您确保您正在运行的版本中端点的装饰器定义为@app.post
,并且没有其他端点使用具有相同的路径>@app.get
。此外,请确保端点内没有发生任何无意的重定向
,因为这可能是导致该响应状态代码的另一个可能原因。为了将来参考,当从POST
重定向到GET
请求时,响应状态代码必须更改为303
,如下所示 此处。另外,您可以尝试使用通配符*
允许所有 HTTP 方法(即allow_methods=['*']
)并查看其工作原理(即使它不应该)与此有关)。最后,这也可能与您运行应用程序的托管服务的配置有关;因此,研究一下也可能是件好事。To start with, your code seems to be working just fine. The only part that had to be changed during testing it (locally) was the URL in fetch from
/post/submit_post
to (for instance)http://127.0.0.1:8000/post/submit_post
, but I am assuming you already changed that using the domain name pointing to your app.The
405 Method Not Allowed
status code is not related to CORS. IfPOST
was not included in theallow_methods
list, the response status code would be400 Bad Request
(you could try removing it from the list to test it). From the reference above:Thus, the 405 status code indicates that the
POST
request has been received and recognized by the server, but the server has rejected that specific HTTP method for that particular endpoint. Therefore, I would suggest you make sure that the decorator of the endpoint in the version you are running is defined as@app.post
, as well as there is no other endpoint with the same path using@app.get
. Additionally, make sure there is no any unintentionalredirect
happening inside the endpoint, as that would be another possible cause of that response status code. For future reference, when redirecting from aPOST
toGET
request, the response status code has to change to303
, as shown here. Also, you could try allowing all HTTP methods with the wildcard*
(i.e.,allow_methods=['*']
) and see how that works (even though it shouldn't be related to that). Lastly, this could also be related to the configurations of the hosting service you are running the application; thus, might be good to have a look into that as well.这是一个老问题,此处进行了描述。您的请求中需要
Access-Control-Request-Method: POST
标头。It's and old issue, described here. You need
Access-Control-Request-Method: POST
header in your request.