How do I post files/images to a service in appfarm?

I’m struggling setting up a service in Appfarm that accepts an image or file as input to a service endpoint. One of my coded components can automatically POST to a service when user drops/copy paste or upload an image. I would like to store that image in the datastore and send back the URL to the component. Are there any examples for how to set up such a service? (eg ‘Content-Type’: ‘image/png’, )

Hi! We do have an example service for sending PDF files as “base 64” strings to Appfarm. See “Services” section in Showroom. However, it has not been documented in docs yet for the PDF case, since it seems we do have some charset issues with PDFs coming in as base64 strings. However, you could try this approach with image/png!

If sending files as base64 strings to Service Endpoints with POST is not the way to go, let us now. There are other ways to post images/files as well, such as doing a POST with “multipart/form-data” but we have not investigated this yet (not any known cases created in Appfarm with this yet).

Thanks! I’ll have a look at the example. As for the return value; the URL to the image at appfarm, I guess I need to store a lookup value to another service …/getImage/:imageId rather than the fileContent url (https://storage.googleapis.com…) of the image that is returned when the image is sucessfully uploaded? The fileContent url is not a permalink is it? By “store” I mean embedding the image URL in the document produced by the coded component.

Hi! The filecontent URL is permanent, however, if it should be accessible from “outside” Appfarm, the “Create File Object” must have the “Public” flag set:
image

Hi. The example you provided did’t work in my case. Probably because the component (Editor.js) executes the request as multipart/form-data I will have to figure out how to parse the incoming request first.

If sending in “multipart/form data” to our endpoint, that body is accessible! It’s just converted to JSON. If you try posting that file to e.g. https://webhook.site/, you may have a look at how that body looks, or you may try mapping the whole body to a string in the service endpoint, and log it to console to see how it looks. Once you know the structure, it should be easier.

It might be that we are missing something here, but my hypothesis is that is should be possible.

I could not get this working in a service, but I understand Appfarm will have better support for binary data in the future. The solution for my particular problem was solved, passing the data directly from the coded component to Appfarm (see # How do I send a file object from a coded component to the datastore )

2 Likes

Hello @kristian, any update here on how to push files to AF services ?
I tried to use the example you provided in showroom, but none of the mimeType I tried are working (pdf/png/jpeg). Files are correctly created, but not usable at all.

Hi!

It should be working now, with the following approach (example):

  1. The Service Endpoint: Use a body parser, and then map the rawBodyData to a service variable:

  2. The Action executed by the Service Endpoint: Use a Create File Object with the following settings (note the encoding here…):

I believe the request towards the service endpoint (1) should have Content-Type: text/plain; charset=latin1. Here’s an example setup of an Web Request sending a file to a Service Endpoint.

Hi Kristian,
Thanks for the answer, but on my side, I don’t see any Custom File Content Encoding option in the create file object node, is this something that will be available in the next release ?

Ah, yes, new feature under feature flag.

DM me the solution name and I’ll enable!