• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Passing multiple binary file streams to a micro-service

 
Bartender
Posts: 1829
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a client where, currently, she is passing multiple file paths (on the same server) to a micro-service. That micro-service then emails those documents after reading the paths (JavaMail API).

My question is, to make the service more REST-like, how would I best send MULTIPLE binary documents themselves (using Base64 Encoding, etc.) rather than the file paths?

I already understand how I would Base64 the binary documents and can easily do that, but my primary concern is that since all the documents (one or more) would end up in the Request BODY, I need a way to parse them into separate files for emailing.

My idea would be use a UUID between each binary file document on the client side so that on the server side, I would know where one file stops and the next one begins.

Is there a better way to go about this?

Thanks,

-- mike
 
Marshal
Posts: 22453
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what multipart form-data is for. It uses a boundary between parts (usually a UUID or something similar, but it can technically be anything), and parts can be both files and simple values. Files can be base64 encoded if necessary.

For instance, with content type multipart/form-data; boundary=9fa69c0b-9d14-4e58-92be-599475860bef:
Here, each part starts with --<boundary>, then its headers (one of them needs to be Content-Disposition with value form-data and at least the part name as parameter), then an empty line (like in HTTP headers) and then the value. After the last part comes a line --<boundary>--<\r\n>. Of course you don't want to build that yourself; fortunately, just about any proper HTTP client has support for this.

On the server side, you can receive these parts "low-level" using HttpServletRequest's getPart and/or getParts methods. Frameworks like JAX-RS and Spring MVC have their own abstractions (probably built on top of Part).
 
Mike London
Bartender
Posts: 1829
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:That's what multipart form-data is for. It uses a boundary between parts (usually a UUID or something similar, but it can technically be anything), and parts can be both files and simple values. Files can be base64 encoded if necessary.

For instance, with content type multipart/form-data; boundary=9fa69c0b-9d14-4e58-92be-599475860bef:
Here, each part starts with --<boundary>, then its headers (one of them needs to be Content-Disposition with value form-data and at least the part name as parameter), then an empty line (like in HTTP headers) and then the value. After the last part comes a line --<boundary>--<\r\n>. Of course you don't want to build that yourself; fortunately, just about any proper HTTP client has support for this.

On the server side, you can receive these parts "low-level" using HttpServletRequest's getPart and/or getParts methods. Frameworks like JAX-RS and Spring MVC have their own abstractions (probably built on top of Part).



Thanks!
 
Rob Spoor
Marshal
Posts: 22453
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic