首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过httpx传递带有名称的文件的表单

如何通过httpx传递带有名称的文件的表单
EN

Stack Overflow用户
提问于 2022-07-11 09:17:31
回答 1查看 320关注 0票数 1

我试图通过httpx将数据发送到戈滕贝格容器。

代码语言:javascript
复制
r = httpx.post(
     "http://doc-to-pdf:3000/forms/chromium/convert/html",
     files={
         "index.html": file_bytes,
     },
     params={
         "marginTop": 0.4,
         "marginBottom": 0.45,  # 0.39 minimum for appending page number
         "marginLeft": 0.1,
         "marginRight": 0.1,
     },
)

但我错了

代码语言:javascript
复制
('Error from gotenberg, %r , %r', 400, b"Invalid form data: form file 'index.html' is required")

知道为什么没有通过httpx传递文件名吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-21 14:47:28

https://www.python-httpx.org/quickstart/搜索“发送多部分文件上载”

代码语言:javascript
复制
>>> files = {'{file}': open('report.xls', 'rb')}
>>> r = httpx.post("{url}", files=files)
>>> print(r.text)
{
  ...
  "files": {
    "{file}": "<... binary content ...>"
  },
  ...
}


Where {file} is the name of field in your {url}-endpoint schema
i.e: 
@router.post({url})
def test_file(
    {file}: UploadFile
):
    pass
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72936206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档