简体   繁体   English

如何通过 httpx 传递带有 nemes 的文件的表单

[英]how to pass form with files with nemes via httpx

I am trying to send data to gotenberg container via httpx lib.我正在尝试通过 httpx lib 将数据发送到gotenberg容器。

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,
     },
)

but i got error但我有错误

('Error from gotenberg, %r , %r', 400, b"Invalid form data: form file 'index.html' is required")

Any ideas why filename not passed via httpx lib为什么文件名不通过 h​​ttpx lib 传递的任何想法

https://www.python-httpx.org/quickstart/ Search there "Sending Multipart File Uploads" https://www.python-httpx.org/quickstart/在那里搜索“发送多部分文件上传”

>>> 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM