Uploading Documents to Paperless NGX via API
A quick note about uploading documents to Paperless NGX via the API: You need to make sure that your multipart form document part includes the filename attribute.
This is automatically done by a browser when it’s using a file field to pull the file, but if you’re constructing the multipart form manually, the API will reject an upload without the filename with the following message:
The submitted data was not a file. Check the encoding type on the form.
The error message is rather misleading, but that’s an issue with the upstream DRF library, not Paperless NGX itself.
If you’re doing the upload in Rust via reqwest
, you’ll want to do something like the following:
fn build_paperless_upload_form(document_response: Response, title: String, filename: String) -> Form {
let document_part = Part::stream(document_response).file_name(filename);
let form = Form::new()
.part("document", document_part)
.text("title", title);
form
}
This particular snippet is creating a reqwest::multipart::Form
from a reqwest::Response
, which allows streaming the response body (which in this case is a PDF download) directly to the Paperless upload API instead of needing to save it to an intermediate file somewhere. Notice the file_name
call, where we’re explicitly adding the file name to the document part. That’s the important piece, and without it DRF (and thus Paperless NGX) will choke and throw a validation error.
Figuring this out ate a decent chunk of time, so hopefully this saves you some debugging time.