Skip to content

Commit e40e246

Browse files
ProxymanBotgitbook-bot
authored andcommitted
GITBOOK-762: No subject
1 parent ae66250 commit e40e246

1 file changed

Lines changed: 62 additions & 1 deletion

File tree

scripting/snippet-code.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ Please open a ticket at [https://github.com/ProxymanApp/Proxyman](https://github
3535
* [Comment & Highlight with color](snippet-code.md#comment-and-highlight-with-color)
3636
* [Abort (close) the request/response connection (Like Block List Tool)](snippet-code.md#abort-the-request-response-like-block-list-tool)
3737

38+
#### Multipart/form-data
39+
40+
* [Read, add, update, or remove a part of multipart/form-data](snippet-code.md#multipart-form-data-1)
41+
3842
## 3. Addons 
3943

4044
* [Base64 Encoding/Decoding or atob/btoa](snippet-code.md#use-base64-addon)
@@ -95,7 +99,7 @@ Please open a ticket at [https://github.com/ProxymanApp/Proxyman](https://github
9599

96100
* [Manually read system environment variables](snippet-code.md#reload-system-environment-variables)
97101

98-
## Websocket
102+
## 12. Websocket
99103

100104
* [Change the Websocket URL, Headers](snippet-code.md#change-websocket-url-requests-response-header)
101105

@@ -292,6 +296,63 @@ function onResponse(context, url, request, response) {
292296

293297
```
294298

299+
#### Multipart/form-data
300+
301+
From Proxyman macOS 6.6.0 or later, you can read, add, delete a multipart/form-data
302+
303+
```javascript
304+
function onRequest(context, url, request) {
305+
// Only run for multipart requests
306+
if (!request.multipart) {
307+
return request;
308+
}
309+
310+
var parts = request.multipart;
311+
312+
// 1) List parts (debug)
313+
console.log("Multipart parts:", parts);
314+
315+
// 2) Modify a text part by name
316+
for (var i = 0; i < parts.length; i++) {
317+
if (parts[i].name === "text_field") {
318+
var p = parts[i];
319+
p.body = "Updated text from script";
320+
parts[i] = p;
321+
}
322+
}
323+
324+
// 3) Modify a file part by name (binary body as [UInt8])
325+
for (var i = 0; i < parts.length; i++) {
326+
if (parts[i].name === "file_field") {
327+
var p = parts[i];
328+
p.body = [72, 101, 108, 108, 111]; // "Hello"
329+
p.fileName = "hello.txt";
330+
p.contentType = "text/plain";
331+
parts[i] = p;
332+
}
333+
}
334+
335+
// 4) Add a new text part
336+
parts.push({
337+
name: "new_field",
338+
body: "Added by script"
339+
});
340+
341+
// 5) Remove a part by name
342+
var filtered = [];
343+
for (var i = 0; i < parts.length; i++) {
344+
if (parts[i].name !== "remove_me") {
345+
filtered.push(parts[i]);
346+
}
347+
}
348+
349+
// Apply updates
350+
request.multipart = filtered;
351+
352+
return request;
353+
}
354+
```
355+
295356
#### Map a local file to Response's Body like Map Local Tool (Proxyman 2.25.0+)
296357

297358
It's a convenient way to directly set a local file to a Body by using `bodyFilePath` property

0 commit comments

Comments
 (0)