Skip to content

Commit 0e57089

Browse files
authored
Merge pull request #15 from jreyesr/feat/batch-files
Add the ability to send files that vary per CSV row
2 parents 0011636 + 4eb5a73 commit 0e57089

11 files changed

Lines changed: 122 additions & 4 deletions

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
The Batch Requests plugin for [Insomnia](https://insomnia.rest) adds the ability to send a request repeatedly, changing parts of every request by variable data, taken from a CSV file. For every response, some data can be collected and added to the CSV file.
66

77
- Repeatedly send a request by reading data from a CSV file
8+
- Send files too by including their file path in the CSV file
89
- Extract data from JSON responses (or other sources, such as the response headers, status code and time taken) and write it back to the CSV file
910
- Works well if not using the plugin (when sending the request manually)
1011
- Add a delay between each request
@@ -23,6 +24,9 @@ Go to the `Application>Preferences` menu in Insomnia, then go to the `Plugins` t
2324

2425
## Usage
2526

27+
> **Note**
28+
> Want to send files in your repeated requests? See [below](#sending-files). The `Batch` tag that is described in this section sends text values only
29+
2630
The plugin adds a template tag to mark the places that you want to replace. To add it, press `Ctrl+Space`, search for the `Batch` tag and press `Enter`. Then, double click the tag to configure it. The tag can be inserted anywhere in the request (e.g. in the URL, query parameters, headers, or body)
2731

2832
![A screenshot showing a template tag that marks a replacement location. It specifies the CSV column that will be used and a sample value that will be sent when manually sending the request](images/templatetag.png)
@@ -55,6 +59,44 @@ On the plugin dialog, you should:
5559
5. Click the `Run!` button at the bottom of the dialog. It will only become active when you have chosen a file and (if any outputs exist) completely filled all Outputs.
5660
6. Click the `Save` button to write the extracted data back to the CSV file, if you need it. Wait until all requests have been performed (as indicated by the progress bar) before clicking this button.
5761

62+
### Sending files
63+
64+
Since `v1.5.0`, it's possible to also send _files_ that vary on each request, just like before it was possible to send _text_ that varied.
65+
66+
For example, this could be useful to upload several files at once to Google Drive: just prepare a CSV that lists the paths to the files and any other data that must vary per file (e.g. the file's title), and then run the request several times.
67+
68+
The CSV must look like this (it must contain a column, here called `fname`, that lists absolute file paths):
69+
70+
![a CSV file that has a column that contains absolute file paths](images/csv_with_files.png)
71+
72+
Then, create a request on Insomnia. Set its Body to Multipart. Add a new field to the body. Insert a `Batch (File)` template tag: place the cursor in the Value of the field, press `Ctrl+Space`, search for `Batch (File)`, and insert it:
73+
74+
![a screenshot of the Batch (File) template tag being offered](images/file_templatetag.png)
75+
76+
> **Warning**
77+
> Do _not_ change the field's type to File! Leave it as Text, which is the default. If you change it to File, it isn't possible to insert template tags, since Insomnia only shows the file picker button, so you'd be forced to choose one single file for all requests
78+
79+
Click on the newly-added tag to open its configuration dialog:
80+
81+
![a screenshot of the Batch (File) tag's config dialog](images/file_templatetag_config.png)
82+
83+
When configuring the tag, set the following two values:
84+
85+
- The name of the CSV column that will be replaced in this tag's location. Copy it from the first line of the CSV file, exactly (including capitalization)
86+
- A sample value. This value will be used when sending the request manually. This is the value that you would have to edit manually if this plugin did not exist.
87+
88+
Click the Done button to save changes. The tag should now look like this:
89+
90+
![a screenshot of the Batch (File) template tag being used in a request](images/file_templatetag_used.png)
91+
92+
From now on, the process continues as normal (see [the Usage section above](#usage)). Click the dropdown menu for the request, select the Batch Requests option, select a CSV file, configure any desired outputs, and click Run. Any fields whose content is a Batch (File) template tag will be replaced with the _contents_ of the file whose path is contained in the configured column of the CSV file. Requests will then be sent as normal. For example, this is the third request sent with the CSV file that was shown above:
93+
94+
![a screenshot of the Insomnia timeline showing a request that includes a binary file, which was sent using the Batch Requests functionality](images/timeline_with_file.png)
95+
96+
If you send the request manually (e.g. by clicking the Run button, or by pressing `Ctrl+Enter` or `F5`), the file that will be sent will be the one that was configured on the template tag's **Sample Data** field:
97+
98+
![a screenshot of the Insomnia timeline showing a request that includes a binary file, which was sent manually](images/timeline_file_manual.png)
99+
58100
### Sources of output data
59101

60102
![a closeup of the UI, showing the Outputs section, with one output of each type (response body, response header, response status code, and request elapsed time)](images/output_datasources.png)

images/csv_with_files.png

10.3 KB
Loading

images/file_templatetag.png

20 KB
Loading

images/file_templatetag_config.png

29.1 KB
Loading

images/file_templatetag_used.png

19.3 KB
Loading

images/timeline_file_manual.png

115 KB
Loading

images/timeline_with_file.png

88.1 KB
Loading

main.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,38 @@ import { createRoot } from 'react-dom/client';
33

44
import BatchDialog from './components/BatchDialog';
55
import BatchDialogSettings from './components/BatchDialogSettings';
6-
import { templateTags } from './tags';
6+
import { templateTags, MAGIC_FILE_FIELD_PREFIX } from './tags';
77

8+
9+
module.exports.requestHooks = [
10+
/*
11+
Intercepts outgoing requests, and then:
12+
1. Finds any fields in the req body whose value starts with %INSOMNIA_PLUGIN_BATCH_REQUESTS%
13+
2. Force-converts them into File fields (they'll be coming in as Text fields)
14+
3. The file that will be sent on that field will be the rest of the text value
15+
(i.e. everything that came _after_ %INSOMNIA_PLUGIN_BATCH_REQUESTS%)
16+
17+
This hook works in concert with the Batch (File) template tag (see tags.js),
18+
which generates values with the requisite magic prefix.
19+
*/
20+
context => {
21+
const body = context.request.getBody();
22+
if(body.mimeType === "multipart/form-data" && body.params) {
23+
for(let param of body.params) {
24+
if(param.value.startsWith(MAGIC_FILE_FIELD_PREFIX)) {
25+
param.type = "file"
26+
param.fileName = param.value.replace(MAGIC_FILE_FIELD_PREFIX, "");
27+
console.debug(`[batchFile.hook] Switching field ${param.name} to File field, originalFilename=${param.value}, filename=${param.fileName}`)
28+
}
29+
}
30+
}
31+
context.request.setBody(body);
32+
}
33+
];
34+
35+
36+
// Provides the context menu action that appears when opening the dropdown menu
37+
// for a request, which is the main entrypoint to the plugin's functionality
838
module.exports.requestActions = [{
939
label: 'Batch Requests',
1040
icon: 'fa-repeat',

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"repository": {
66
"url": "https://github.com/jreyesr/insomnia-plugin-batch-requests"
77
},
8-
"version": "1.4.0",
8+
"version": "1.5.0",
99
"author": {
1010
"name": "jreyesr",
1111
"url": "https://github.com/jreyesr"

0 commit comments

Comments
 (0)