Process images as multipart/form-data


Hello all, we are back with an interesting post. We had a requirement recently where we have to download an image from an endpoint and then post it to backend as an attachment (image/jpeg) along with the image name as multipart/form-data. The backend is hosted as an OPEN API specification.

So far the requirement is clear right..then what are some of the thoughts on this?


Our thought process on this:

1) Tools we have   -->   IIB, Datapower

2) Download an image from endpoint     -->    Ok where do we get this endpoint details, do we have any security setups to be done? In which format will it be downloaded? If it is image/jpeg, how can my tools handle this data?

3) Post it to backend as an attachment (image/jpeg)    -->  As we are core into middleware technologies, the first question that came to mind is how and in which format we can download an image and process it to backend…since buffering the image in any local directories is not a valid option.

4) And how can we send the image name?  -->  When verified the backend API specification, the backend will not accept any content in the post body, everything has to be with in the headers of multipart/form-data.

We did handle attachments in our past experience as a MIME attachments using email nodes in IIB. Also familiar that SOAP messages have this kind of capability. But our requirement here is a REST call.

So the biggest challenge in achieving this requirement was finding a way to send the image name to backend along with posting the image with proper data format (image/jpeg).

Pre-requisites:

Meeting this requirement was not as straight forward approach as this involves in understanding the multipart/form-data, how we have to set the headers and image name.

1) multipart/form-data: each value is sent as a block of data ("body part"), with a user agent-defined delimiter ("boundary") separating each part. The keys are given in the Content-Disposition header of each part.

2) Boundary:  The key things in here are the Content-Type, Content-Disposition, Content-Transfer-Encoding and data.

3) Content-Disposition is where we have to send the field name which backend expects the image name. In our case, backend retrieves this detail from field ‘fileName’. So we have to set the name as filename and after that pass the filename.


More info on the multipart/form-data can be found in the HTML5 spec:

Because of ease of handling image data (downloading the image) in IIB, we are showing the implementation in IIB.

Implementation in IIB:

Please note that we are only showcasing the happy path implementation of the requirement here:

Step 1: Once the service is invoked, use the endpoint (received in incoming message, here for happy path scenario I am using that endpoint in HTTPRequest node itself) and then use HTTP Request node to download an image. At HTTP Request node, use BLOB as the parser for response message parsing.



Step 2: Use below esql to parse the BLOB (output from HTTP Request node, which is image in BLOB representation) and populate the headers as per multipart/form-data and backend requirement.



Step 3: Call the backend service.
That’s it, we are able to meet our requirement!!! 


Thank you for visiting our blog. Stay subscribed for more updates!


Comments

Popular posts from this blog

Installing IBM API Connect v2018.4.1.x on VMware

Introduction to IBM Turbonomics

Configuring monitoring events in IIB