How to use azure logic app action to download files in browser - azure

I originally created a logic app that would, given a JSON payload, run a stored procedure, transform the results into a CSV table and then email the CSV to a specified email account. Unfortunately requirements changed slightly and instead of emailing the csv they want it to download directly in the browser.
I am unable to get the HTTP response action to tell the browser to download the file using the Content-Disposition header. It looks like this is pulled out of the request by design. Is anyone aware of another action (perhaps a function?) that could be used in place of the HTTP response to get a web browser to download the file rather than returning it as text in the response body?

It does indeed seem to be the case that the Response action doesn't support the Content-Disposition header for some reason. Probably the easiest workround is to proxy the request through a simple HTTP-triggered Azure Function with CORS enabled (or an API on your server) that just fetches the file from the Logic App and then returns it with the Content-Disposition header attached.
NB. Don't rely on <a download="filename"> - most browsers that support the download attribute only respect it for same-origin requests.

Related

Serving a HTTP request response as a dialog response - Composer Framework

We are developing a chatbot to handle internal and external processes for a local authority. We are trying to display contact information for a particular service from our api endpoint. The HTTP request is successful and delivers, in part, exactly what we want but there's still some unnecessary noise we can't exclude.
We specifically just want the text out of the response ("Response").
Logically, it was thought all we need to do is drill down into ${dialog.api_response.content.Response} but that fails the HTTP request and ${x.content} returns successful but includes Tags, response and the fields within 1.
Is there something simple we've missed using composer to access what we're after or do we need to change the way our endpoint is responding 2? Unfortunately the MS documentation for FrwrkComp is lacking to say the very least.
n.b. The response is currently set up as a (syntactically) SSML response, this is just a test case using an existing resource.
Response in the Emulator
Snippet from FwrkComp
Turns out it was the first thing I tried just syntactically correct. For the case of the code given it was as simple as:
${dialog.api_response.content[0].Response}

How to download multiple files using ExpressJS one API call

I'm quite new to expressjs and I'm developing a web application which acts as an API application. There is a react frontend application also. When a button is clicked in the client app it will send an API call to the backend app and will download a file. That scenario is working fine.with the following code.
const file = `${__dirname}/upload-folder/dramaticpenguin.MOV`;
res.download(file); // Set disposition and send it.
});
But now I have a requirement to download multiple files from a button click. How can I do that .
Can someone help me here.
An HTTP response can only have one file. Really, "downloading a file" in HTTP means serving a response with a Content-Disposition: attachment header, to hint to the client that this response should be saved to the filesystem instead of rendered in the browser.
To download multiple files, you want the client code to initiate multiple HTTP requests (probably to different URLs), and the server can respond to each request with a different file. Note that many browsers will refuse to download multiple files in response to a single user action (for fear of flooding the user with unwanted files) or will at least prompt for confirmation before doing so.
If you cannot change your client-side code to make multiple requests, you will need to package your files inside a single file archive.

How to download XHR files from chromedriver using python?

As shown in the screenshot, I need to download all the XHR files in the network when any call made using python?
First load an URL on the domain you're targeting a file download from. This allows you to perform an AJAX request on that domain, without running into cross site scripting issues.
Next, inject some javascript into the DOM which fires off an AJAX request. Once the AJAX request returns a response, take the response and load it into a FileReader object. From there you can extract the base64 encoded content of the file by calling readAsDataUrl(). Then take the base64 encoded content and appending it to window, a gobally accessible variable.
Finally, because the AJAX request is asynchronous, enter a Python while loop waiting for the content to be appended to the window. Once it's appended, decode the base64 content retrieved from the window and save it to a file.
This solution should work across all modern browsers supported by Selenium, and works whether text or binary, and across all mime types.

how to set query variables on server response

I am running an express app and in a section I need to pass the page I'm serving some data. I am sending the file with the res.sendFile() function. I would prefer it to be in the form of query parameters, so that the page being sent is able to read them easily.
I am unable to run any templating tool or set cookies since the files are part of a cdn uploaded by users, so the information has to be contained so that it is not easily read by other files also served from my server.
Query parameters can only be sent by doing a redirect where your server returns a 3xx status (probably 302) which redirects the browser to a different URL with the query parameters set. This is not particularly efficient because it requires an extra request from the server. See res.redirect() for more info.
A more common way to give data to a browser is to set a few Javascript variables in the web page and the client Javascript can then just read those variables directly. You would have to switch from res.sendFile() to something that can modify specific parts of the web page before sending it - probably one of the many template engines available for Express (jade, handlebars, etc...).
You could also send data by returning a cookie with the response, though a cookie is not really the ideal mechanism for variables just for one particular instance of one particular page.

How the browser treats static file,downloadable file,json,xml

Very basic question.Here it goes.
The client hits a url in the server.The server can send content in the form of
static files(javascript/html).
xml/json(predominantly the purpose of this file is to return some DATA to the client).
Downloadable file-kinda zip files.For this part the server needs to set the content type property to something to let the client know that it wants this file to download of something.
My question is how does the browser differentiate between the static files and api responses(form of xml/json/string) ??
Thanks,
Gully
HTTP Headers.
There's no such thing as a "file" in HTTP. There are requests and responses, each of which consist of headers and content. The response content may be the contents of what was a "file" on the server, and may be intended to be treated as a "file" on the client (such as downloading a .zip file), but the response itself is not a file. The way that the server indicates to the client that something should be a file is through the HTTP headers.
Specifically the two headers you're talking about are:
Content-Type
Content-Disposition
The first tells the client (browser) what kind of data it's receiving. There are lots of examples, and most browsers understand what to do with most common types. The second can be used to suggest to the client that the content should be saved as a file rather than displayed. For example, the Content-Type might be for an image, and by default a browser will just display an image. But you can add a Content-Disposition header to indicate that the image is an "attachment" and even suggest a file name for it, instructing the browser to save the file (or prompt the user asking to save the file) instead of displaying it.

Resources