“GET / HTTP/1.1” 405 Method Not Allowed

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

"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&#39;t use my Post request in FastAPI , I used the Post request but the terminal displayed the Get request and I don&#39;t know why.
And when I used the Body(...) to declare the dict type but it looked didn&#39;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&#224;o bằng c&#225;ch sử dụng FastAPI&#39;s Body dependency. ... được sử dụng để chỉ định rằng tham số 
# n&#224;y l&#224; bắt buộc v&#224; kh&#244;ng thể bỏ qua.
@app.post(&quot;/create&quot;)
def create_posts(payload: dict = Body(...)):
    # It will extract all the field from the body to the dict
    print(payload)
    return {&quot;message&quot;: f&quot;successfully created posts: {payload[&#39;title&#39;]}&quot;}

@app.get(&quot;/&quot;)
def get_data():
    return {&quot;message&quot;: &quot;Hello World&quot;} 

Here is my error

INFO:     Will watch for changes in these directories: [&#39;T:\\FastAPI&#39;]
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 - &quot;GET /create HTTP/1.1&quot; 405 Method Not Allowed
INFO:     127.0.0.1:60812 - &quot;GET /cart.json HTTP/1.1&quot; 404 Not Found
INFO:     127.0.0.1:60812 - &quot;GET /create HTTP/1.1&quot; 405 Method Not Allowed```

</details>


# 答案1
**得分**: 2

问题在于你的`get`方法在localhost:8000/上定义(如果你使用默认模式下的uvicorn运行应用程序)

```python
@app.get("/")
def get_data():
    return {"message": "Hello World"}

所以你应该发送一个请求,像这样

“GET / HTTP/1.1” 405 Method Not Allowed

而不是

“GET / HTTP/1.1” 405 Method Not Allowed

你应该使用POST代替

“GET / HTTP/1.1” 405 Method Not Allowed

或者定义类似这样的东西

@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(&quot;/&quot;)
def get_data():
    return {&quot;message&quot;: &quot;Hello World&quot;}

So you should send a request like

“GET / HTTP/1.1” 405 Method Not Allowed

Instead of

“GET / HTTP/1.1” 405 Method Not Allowed

You should use POST instead

“GET / HTTP/1.1” 405 Method Not Allowed

Or define something like

@app.get(&quot;/create&quot;)
def get_data():
    return {&quot;message&quot;: &quot;Hello World&quot;}

答案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.

huangapple
  • 本文由 发表于 2023年4月19日 17:34:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052942.html
匿名

发表评论

匿名网友

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

确定