简体   繁体   中英

how to pass form with files with nemes via httpx

I am trying to send data to gotenberg container via httpx lib.

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

https://www.python-httpx.org/quickstart/ Search there "Sending Multipart File Uploads"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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