英文:
"GET / HTTP/1.1" 405 Method Not Allowed
问题
I had an issue with that I cann't use my Post request in FastAPI , I used the Post request but the terminal displayed the Get request and I don't know why.
And when I used the Body(...) to declare the dict type but it looked didn't work
from fastapi.params import Body
from pydantic import BaseModel
app = FastAPI()
# payload : input parameter of the function
# dict: type of data
# được truyền vào bằng cách sử dụng FastAPI's Body dependency. ... được sử dụng để chỉ định rằng tham số
# này là bắt buộc và không thể bỏ qua.
@app.post("/create")
def create_posts(payload: dict = Body(...)):
# It will extract all the field from the body to the dict
print(payload)
return {"message": f"successfully created posts: {payload['title']}"}
@app.get("/")
def get_data():
return {"message": "Hello World"}
Here is my error
INFO: Will watch for changes in these directories: ['T:\\FastAPI']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [14464] using WatchFiles
INFO: Started server process [3468]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: 127.0.0.1:60812 - "GET /create HTTP/1.1" 405 Method Not Allowed
INFO: 127.0.0.1:60812 - "GET /cart.json HTTP/1.1" 404 Not Found
INFO: 127.0.0.1:60812 - "GET /create HTTP/1.1" 405 Method Not Allowed```
<details>
<summary>英文:</summary>
I had an issue with that I cann't use my Post request in FastAPI , I used the Post request but the terminal displayed the Get request and I don't know why.
And when I used the Body(...) to declare the dict type but it looked didn't work
``` from fastapi import FastAPI
from fastapi.params import Body
from pydantic import BaseModel
app = FastAPI()
# payload : input parameter of the function
# dict: type of data
# được truyền vào bằng cách sử dụng FastAPI's Body dependency. ... được sử dụng để chỉ định rằng tham số
# này là bắt buộc và không thể bỏ qua.
@app.post("/create")
def create_posts(payload: dict = Body(...)):
# It will extract all the field from the body to the dict
print(payload)
return {"message": f"successfully created posts: {payload['title']}"}
@app.get("/")
def get_data():
return {"message": "Hello World"}
Here is my error
INFO: Will watch for changes in these directories: ['T:\\FastAPI']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [14464] using WatchFiles
INFO: Started server process [3468]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: 127.0.0.1:60812 - "GET /create HTTP/1.1" 405 Method Not Allowed
INFO: 127.0.0.1:60812 - "GET /cart.json HTTP/1.1" 404 Not Found
INFO: 127.0.0.1:60812 - "GET /create HTTP/1.1" 405 Method Not Allowed```
</details>
# 答案1
**得分**: 2
问题在于你的`get`方法在localhost:8000/上定义(如果你使用默认模式下的uvicorn运行应用程序)
```python
@app.get("/")
def get_data():
return {"message": "Hello World"}
所以你应该发送一个请求,像这样
而不是
你应该使用POST代替
或者定义类似这样的东西
@app.get("/create")
def get_data():
return {"message": "Hello World"}
英文:
Problem is your get method is defined at localhost:8000/ (in case you run the app with uvicorn in default mode)
@app.get("/")
def get_data():
return {"message": "Hello World"}
So you should send a request like
Instead of
You should use POST instead
Or define something like
@app.get("/create")
def get_data():
return {"message": "Hello World"}
答案2
得分: 2
你的代码运行正常。最可能的问题是实际上你没有发送POST请求。
在浏览器中打开http://localhost:8000/docs并尝试请求。
英文:
Your code works just fine. Most probably the only issue is that you in fact don't actually send a POST request.
Open http://localhost:8000/docs in your browser and try the request yourself.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论