【问题标题】:how to pass form with files with names via httpx如何通过 httpx 传递带有 nemes 的文件的表单
【发布时间】:2022-07-21 22:47:28
【问题描述】:

我正在尝试通过 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,
     },
)

但我有错误

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

关于为什么文件名不通过 h​​ttpx lib 传递的任何想法

【问题讨论】:

    标签: python httpx gotenberg


    【解决方案1】:

    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
    

    【讨论】:

      猜你喜欢
      • 2019-10-17
      • 1970-01-01
      • 2012-02-18
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      • 2021-10-10
      • 2013-09-28
      • 2023-03-22
      相关资源
      最近更新 更多