如何通过httpx传递带有文件名的文件



我正在尝试通过httpx库发送数据到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")

知道为什么文件名没有通过httpx lib

传递吗?

https://www.python-httpx.org/quickstart/搜索quot;发送多部分文件上传&;

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

最新更新