Answer HttpResponse with JSON+ PDF?

Hi,

I am working on document generation.
I need to add a service in API to answer the PDF generated + Json (with a lot of comment about the generation : what cannot be included, what not available, etc...).

For now, I only use a way to answer the PDF document like this :
Ok(pdf: Vec<u8>) => Ok(HttpResponse::Ok().content_type("application/pdf").body(pdf)),

I use header for my answer that is a application/pdf.
After reading documentation, I discover we can use "multipart" header for request. But not seems to be available for answer ?

I was thinking to add my Vec<u8> data for the pdf in some field of json but I think I will have maybe problem with character to escape like ".

So I am searching a way and I wonder if there is already a way for this kind of feature ? Because I think is really usefull to answer many type of data.

Have you already use multipart for an answer to a request ?
I discover this crate, trying to understand it, if for the answer it can be used... https://docs.rs/multipart/latest/multipart/server/index.html

You can use multipart/form-data as the content type of a http response. Which http server framework (e.g. actix-web, axum, etc.) are you using?

I don't think multipart is great, it generally requires you to base 64 encode binary data. I'd recommend two requests for the metadata and data if that doesn't break things, or wrap them both into a zip or tar.gz, something like that.

actix-web !

Yes, it is a proposition I made, but they prefer only one request with one answer...
And I remember someone was not fan of passing pas base64 too...
Maybe there is another way ?

Hmmm, actix-multipart exists but looks like it only supports reading them (for form submissions with file uploads)

Theres a bunch of results for a crate search for multipart, but nothing jumped out.

If this is going to a browser native download you can pretty much only use an archive format, but if it's an API it's pretty much up to you exactly how it works, but essentially including multiple files in one is an archive one way or the other.

For example you can define a very very basic archive format like:

{tag} {version} {meta size}\n
{meta}\n
{data}

Where tag and version just let you check you're reading the right thing and be able to make changes respectively, eg.

metadoc 1 18
{"author":"Alice"}
PDF...

Depending on the size of the JSON you could put in in a response header.

2 Likes

Thank you for your tips. I will look that !

There's also multipart/mixed. It's more common in emails, browsers don't actively support it and you'd need some JS library to decode them.
But OP didn't say what kind of client they're targeting.

Not sure what part you're replying to: if it's about uploading files then I was saying the use that is implemented by that library.

While multipart encoding could maybe, in theory, use a binary content-encoding for parts, I'm not aware of any libraries that support this. When I used a (JavaScript) multipart library to assemble an email body it would automatically base64 or (worse, for binary) urlencode the content, and the quick search for a crate didn't find an appropriate one either. This isn't that surprising, as the only real use for them is email bodies, and those are still(!) only supporting 7-bit ASCII for "compatibility"

Regardless, multipart is basically just an archive format for http responses, if you don't need the extra header metadata you may as well use zip or tar or whatever else is convenient.

hi,

don't know about actix-web, but i do use rocket and rocket-multipart for a similar requirement.

you can see how i handle my requests and generate such answer here.

Thank you very much.

For now, I use the header to put my json and it works.
I will discuss this solution with my team to see what we can do. Because in future, maybe multipart answer will be more generic.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.