fastapi hello world
时间: 2023-07-25 12:29:03 浏览: 64
好的,下面是使用 FastAPI 框架编写的 "Hello, World!" 程序:
```python
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"Hello": "World"}
```
运行这个程序后,在浏览器中访问 http://localhost:8000/,即可看到输出结果:{"Hello": "World"}。
相关问题
fastapi返回hello world
以下是使用 FastAPI 返回 "Hello World" 的示例代码:
```python
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
```
运行代码后,浏览器访问 http://localhost:8000 即可看到返回的 "Hello World"。
fastapi访问/返回hello world
可以使用以下代码实现基本的 FastAPI 应用程序来访问/返回“Hello, World!”:
```python
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello, World!"}
```
将以上代码保存为 `main.py` 文件,然后运行以下命令启动 FastAPI 应用程序:
```
uvicorn main:app --reload
```
现在访问 `http://localhost:8000/` 应该会看到一个 JSON 响应,其中包含 `{"message": "Hello, World!"}`。