Question

How to change the return response from zapier requests?

  • 20 September 2022
  • 8 replies
  • 41 views

Currently we are trying to retrieve a file from an API call. The return type of response.content from the API is binary/octet-stream but from the zapier its transforming as string. We tried the Content-Type in the zapier requests header but its throwing an error. With the string format some characters are not encoding properly. so we want to retrieve it same as the API send. How can we achieve this?


8 replies

Userlevel 3
Badge +4

Hi @Lavarthan !

Are you asking about your Download Files trigger?

Have you tried using `z.stashFile`? I think that’s going to be more helpful than trying to change the return response.

We have some documentation on stashing files here that you might find helpful, and one of the examples even mentions binary/octet. This goes along with other documentation on hydration and dehydration and this example app for files might be helpful as well.

Hi @shalgrim thanks for the reply. But here stashing file will return a URL as response right?. We need to return the same file content as binary/octet-stream here. 

Userlevel 3
Badge +4

Hi @Lavarthan ,

Oh yeah, you’re right. I was thinking (but didn’t make explicit) that then you’d be able to return it kind of like this example to result in a file. Basically a hack where you could then download it this way.

Have you tried setting the raw: true flag as done in that stashing files example? That is also the approach used in the downloadFile hydrator used by the example I just linked above.

 

Yes after I tried the raw:true. I got new error


Error: You passed {raw: true} in request() - the response.content property is not available! Try response.body.pipe() for streaming, response.buffer() for a buffer, or response.text() for string.

In pipe() what should we pass?
Here is my code
 

const response = await z.request(url: API_URL,  method: 'GET',  raw: true,
);return { content: response.body.pipe(NOT_SURE_WHAT_TO_PASS_HERE) };
Userlevel 1
Badge +1

Hey @Lavarthan! Could you please confirm the following details for us?

  • App ID
  • Filename within that app which contains the code you’re working on

The error message you’ve seen here sounds like the code might either be referring to `response` before the promise assigned to it has resolved, or the `response` doesn’t contain a `content` property as the code expects. Once we can take a look at your code within context, we’ll be better positioned to get you pointed in the right direction.

 

In general, I’d suggest modelling your app off these examples:

`newFile.js` is an example trigger which downloads a file from a URL using `z.dehydrateFile()`:

We can see that this method takes two input parameters; a `downloadFile` promise and an object containing the URL of the file. The `downloadFile` promise is defined in the `hydrators.js` file, which is where the HTTP request for the actual file is made:

And the response to that request (the file data) is stashed, so that we return a URL pointing to the stash file:

I hope this helps!

@Christian W

App ID: 167973
filename: download-file.js (/actions/contract/download-files.js)

 

I tried the stashing it returning the URL, but my case want the content of the file instead the URL.

Userlevel 1
Badge +1

Thanks for confirming that, @Lavarthan :)

 

It looks like your app differs in a few ways from the example, so I think your next steps should be refactoring the code such that:

  1. The `perform` method for the “DownloadFiles” action returns an object containing a property named “file”
  2. The “file” property is populated by a call to `z.dehydrateFile()`
  3. `z.dehydrateFile()` is fed two input parameters; a function to download the file and an object containing the URL of the file to be downloaded (more info on this is here)

The function to download the file (named `hydrators.downloadFile` in the example) should make an asynchronous HTTP call using `z.request()` and can optionally stash the file and return a URL. Since you’ve mentioned that you’d prefer the actual file object is returned to the Zap, you can omit the `raw: true` property and call to `z.stashFile()`.

 

Once complete, the result will look like the examples given in this tutorial which I believe demonstrates the outcome you’re looking for :)

Thanks for the answer @Christian W . I tried the method you suggested and got the same response give in this tutorial. But i’m still facing the encoding issue. For example I’m trying to retrieve pdf file which has an image. In this case image is missing. Seems like while zapier retrieving the content from the API request it is missing it?
I have pushed my latest version of code. Will you able to take a look?

Reply