问题标签 [fastapi]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
6319 浏览

javascript - 为什么此端点会出现 Access-Control-Allow-Origin 错误?

我很难理解为什么这个请求给了我一个 CORS 错误。我相信我有服务器和客户端设置来正确使用端点。我可以向邮递员提出相同的请求并且它可以工作,所以我认为我在使用 JS 时做错了什么。服务器使用的是 Python 框架 Fastapi。客户端是 Vue,但请求使用 Axios(即 Javascript)。任何见解将不胜感激。

服务器设置

客户要求

日志

我在 axios 请求标头中明确包含 Access-Control-Allow-Origin ,但似乎无法理解。我究竟做错了什么?谢谢。

0 投票
1 回答
3727 浏览

python - REST API in Python with FastAPI and pydantic: read-only property in model

Assume a REST API which defines a POST method on a resource /foos to create a new Foo. When creating a Foo the name of the Foo is an input parameter (present in the request body). When the server creates a Foo it assigns it an ID. This ID is returned together with the name in the REST response. I am looking for something similar to readOnly in OpenAPI.

The input JSON should look like this:

The output JSON should look like that:

Is there a way to reuse the same pydantic model? Or is it necessary to use two diffent models?

I cannot find any mentions of "read only", "read-only", or "readonly" in the pydantic documentation or in the Field class code.

Googling I found a post which mentions

But that seems to have no effect in my use case.

0 投票
7 回答
51002 浏览

python - Python:带有发布请求的 FastAPI 错误 422

我正在构建一个简单的 API 来测试数据库。当我使用 get request 时,一切正常,但是如果我更改为 post,我会收到“无法处理的实体”错误:

这是 FastAPI 代码:

然后,我的请求使用 javascript

也使用 Python

我也尝试解析为 json,使用 utf-8 编码,并更改标题。没有什么对我有用。

0 投票
1 回答
870 浏览

fastapi - 如何解决fastapi中没有属性“路由”?

我按照https://fastapi.tiangolo.com/tutorial/bigger-applications/资源来设计我的应用程序

正确导入所有内容。当我运行 uvicorn main:app --reload 它显示“NO attribures 'routes'”错误我无法找到,我在这里做的错误是什么。任何人都可以帮助我。

0 投票
1 回答
754 浏览

python - Asyncio.create_subprocess_exec NotImplementedError - Fastapi 后台任务

我试图asyncio.create_subprocess_exec在 Fastapi 后台任务中调用,但它不断提高NotImplementedError. run_subprocess 函数在 Fastapi 之外运行时可以正常工作。我在 Windows 中使用 asyncio 循环而不是 uvloop 运行它。

任何人都可以帮忙解决这个问题吗?

谢谢!

0 投票
3 回答
7341 浏览

python - 使用动态键创建 Pydantic 模型模式

我正在尝试为以下 JSON 实现 Pydantic 模式模型。

这是 Pydantic 中的模式对象

我正在解析 AuthorInfoCreate 如下:

我看到以下错误。

我想了解如何更改 AuthorInfoCreate 以便我提到 json 架构。

0 投票
7 回答
15739 浏览

python - FastAPI (starlette) 获取客户端真实IP

我在 FastAPI 上有一个 API,当他请求我的页面时,我需要获取客户端的真实 IP 地址。

我很喜欢使用 starlette Request。但它返回我的服务器 IP,而不是客户端远程 IP。

我的代码:

我做错了什么?如何获取真实 IP(如 Flask request.remote_addr)?

0 投票
4 回答
26708 浏览

python - 如何在 FastAPI 中使用 Pydantic 模型和表单数据?

我正在尝试从 HTML 表单提交数据并使用 Pydantic 模型对其进行验证。

使用此代码

但是,我收到 HTTP 错误:“无法处理的422实体”

等效的 curl 命令(由 Firefox 生成)是

此处请求正文包含no=1&nm=abcd.

我究竟做错了什么?

0 投票
1 回答
17892 浏览

python - 指定主机时 FastAPI/uvicorn 不起作用

我在 Windows 机器上使用 uvicorn 在 Python 中运行 FastAPI 应用程序。当我要么

  1. 在我的 Mac 上运行以下代码,或者
  2. 当我没有为 uvicorn 指定端口时(host从 uvicorn.run 调用中删除参数)
  3. 当我指定端口“127.0.0.1”时,它是我根本不指定主机时使用的主机。

当我在浏览器上转到 0.0.0.0:8080 时,我收到一条错误消息,提示“无法访问此站点”。

我已经检查了我当前的活动端口,以确保我没有遇到冲突,netstat -ao |find /i "listening"并且 0.0.0.0:8080 没有被使用。

我当前的文件配置如下所示:

我的 gunicorn_conf.py 非常简单,只是尝试设置主机和端口:

当我指定主机'0.0.0.0'时,如何让它工作?

0 投票
3 回答
7597 浏览

pagination - fastapi contrib 有 postgresql 的分页器吗?

我阅读了 fastapi contrib 代码。分页仅适用于 mongodb。fastapi 是否也有用于 postgres db 的分页模块?