Sending a JSON-file as part of form-data in Web Request

I need to send a json-object as a file to an external service. I have generated the JSON but no matter what I try the file is not sent from Appfarm. I have tried:

  • Converting the JSON-object to a blob and storing it in a File Object Class and then binding fileObjectClass.__file to the form-data (data type File) in the web request action node.
  • Sending the request using “Run code” action node.
  • Generating the blob directly in the function editor for the form-data in the web request action node. This raises an error saying that Blob is not defined

In all scenarios when inspecting the network tab in developer tools I see that the file is not sent from Appfarm.

What am I doing wrong? Does anyone have any pointers?

Hi Johannes,

Generally, most web services and APIs accept JSON data as an object, where the JSON content is sent directly as part of the HTTP request body rather than requiring a JSON file. Can you confirm that this is not the case with the service you’re integrating with?

When you inspect your request, what do you see under Debug info in the Console?

Did you manage to get the request to work in Postman? (Just to make sure that the issue is your setup in Create, not the request itself.)

Yes I have managed to get the request to work in Postman.

It seems that it might work to send the JSON-structure as a string with data type File, but now I run into issues with CORS when sending from client. If I uncheck Send from client I get Error: connect EADDRNOTAVAIL ::1:80 - Local (:::0)

Any suggestions to how I can solve this?

Hi!

I have done something similar with an XML file once. It was done from a Service, but I believe the same should work in an App. Here are the steps (adjusted for the fact that you are producing a .json instead of a .XML file)

Step 1: Create File Object (action node)

  • Data Source: A runtime only data source (File Object Class) with cardinality one
  • Source Type: Custom
  • Custom File Content: a function returning the JSON structure as a string. E.g. return `{name: “Bob”}`
  • File Name: Myfile.json
  • Mime type: application/json

Step 2: Web Request (action node)

  • Method: POST
  • Body Type: Form Data
  • Body Content (example, this depends on the name of the fields the recipents has specified): Key: fil, Data Type: File, Value: My-file-datasource.File Content

Since the My-file-datasource is a runtime only datasource, the File Content property is in fact a blob (since it is not persisted, i.e. has no URL to refer)

1 Like

Hi, Kristian.

That seemed to do the trick. I have previously tried something very similar where I converted the json object to blob. Also I did not set a filename or mime-type, maybe that was crucial.

However, now my issue is that the result parser times out. I use the same code as given in this article. I tried turning on Send from client in case the timeout only applies to server actions, but then I run into other more confusing issues again where the file is seemingly missing from the request.

Hi,

In that case, I am curious about the content of the result parser. The result parser times out after 2 seconds of pure “code execution time”, which is a lot. Send me a DM on this one.