pythonhttpxgotenberg

how to pass form with files with names 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


Solution

  • 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