Sample code for receiving REST message with file attachment via multipart message content-type header?

Not applicable

I need to be able to make an Apigee proxy which can receive a file attachment from another REST API. I found an old Apigee forum post (see below) which says I'll need a call out policy for this. I'm wondering if anyone knows of existing sample Java or Node.js code that can deal with file attachments in this way.

"REST/SOAP messages with file attachments are classified as multipart messages via the content-type header and custom java callout policies have to be written to handle these types of messages. ExtractVariables policy will work only for a xml/json message."

1 8 2,422
8 REPLIES 8

Hmmm, depends on what you need Apigee Edge to DO.

Apigee Edge can act as a reverse proxy, which means it need not do anything to the message payload or headers. In that case you don't need a Java callout or any special arrangement in your proxy, to make this happen. the proxy will receive the file attachment, and pass it through.

I suspect you might want your proxy to do something with the file attachment. In that case, YES, you probably do want to use a Java callout to handle that message payload, to extract the attachment and... do something with it.

Can you elaborate on just what you want to accomplish?

Thanks, Dino. I believe our plan is to have a Node.js app which will upload the file attachment to AWS S3 and (I think) a JSON payload to AWS DynamoDB. Just wonder if there's any pre-canned Apigee code for dealing with the file attachment.

Gotcha Aaron. Let me see what I can do....

@Aaron Shaver -

Please find attached an Apigee Edge proxy that relies on nodejs to parse an inbound request that contains a multipart/form-encoded POST, with file attachments.

The nodejs logic doesn't actually DO anything with the file attachment. It just counts up the bytes and dumps them in the bit bucket. For your purpose, you'd need to modify that nodejs code to use the AWS SDK to push to S3 (or whatever).

Helpful?

multipart-form.zip

Thanks. I'm still trying to digest your files. Is the idea that the external API would POST tp the proxy endpoint /docs? Hmm, that flow doesn't have steps in it, though. Maybe the main thing is the pass through to the target flow, which sends to the Node server.js?

Yes - correct. There are no policies; the nodejs logic is the thing that parses the payload.

Thanks. So the latest I've discovered is that we want to use Apigee as a mere passthrough and do the heavy lifting on AWS. I assume I could throw your server.js into an EC2 instance running Node in AWS.

Hmmm, maybe. The nodejs logic really doesn't do much "lifting". What it does is parse the multipart form payload, and extract the file streams. I suppose you'd want to pipe that stream data to S3. I inserted comments to that effect in the code. You would need to rely on the AWS SDK to make that happen. (This is pretty easy to do).

At some point, after the file is written to S3, maybe something else in AWS will ready that S3 thing, and do something else. Maybe that's the "heavy lifting".

But as for running the nodejs code that I wrote in AWS EC2... I don't think that would make sense.