mirror of
https://github.com/ProxymanApp/Proxyman.git
synced 2026-04-27 09:05:54 +03:00
[GH-ISSUE #796] Script doesn't allow ArrayBuffer as response.body #791
Labels
No labels
Discussion
Feature request
In Progress...
Plugins
Waiting response
Windows
Windows
bug
duplicate
enhancement
feature
good first issue
iOS
macOS 10.11
question
wontfix
✅ Done
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/Proxyman#791
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @SupremeTechnopriest on GitHub (Feb 19, 2021).
Original GitHub issue: https://github.com/ProxymanApp/Proxyman/issues/796
Originally assigned to: @NghiaTranUIT on GitHub.
Proxyman version? (Ex. Proxyman 1.4.3)
2.18.0macOS Version? (Ex. mac 10.14)
11.2.1Steps to reproduce
Attempt to respond with an ArrayBuffer
Expected behavior
It should send the buffer to the client but instead it is throwing the following error:
❌ Error: Could not parse the body as data. (Content-Type=application/octet-stream)@SupremeTechnopriest commented on GitHub (Feb 20, 2021):
If you can solve this issue I will be dropping Charles and picking up a license to proxyman.
@NghiaTranUIT commented on GitHub (Feb 20, 2021):
@SupremeTechnopriest It's a limitation of JavascriptCore when mapping the JS Data Type to Swift. JavascriptCore doesn't allow passing directly ArrayBuffer to Swift (ref: https://nshipster.com/javascriptcore/)
To workaround it, we should convert ArrayBuffer to Base64Encoded String and set to Response as usual (https://docs.proxyman.io/scripting/script#onresponse-objects-format)
I will write down an example to you 👍
@SupremeTechnopriest commented on GitHub (Feb 20, 2021):
Ah I see. And the client will receive it as an ArrayBuffer when reading the stream with
await res.arrayBuffer()?@NghiaTranUIT commented on GitHub (Feb 20, 2021):
Here is the script. Please try again if you see if it works 😄
Ref: https://docs.proxyman.io/scripting/script#5-notes
@NghiaTranUIT commented on GitHub (Feb 20, 2021):
If you need to send a binary file, you can directly import the file and set it as a response.body (See Snippet Code https://docs.proxyman.io/scripting/snippet-code#binary-file)
@NghiaTranUIT commented on GitHub (Feb 20, 2021):
Unfortunately, JavascriptCore doesn't support
awaitorPromise: /You might directly construct an ArrayBuffer or import it from a file (as I highly recommend) 👍
@SupremeTechnopriest commented on GitHub (Feb 20, 2021):
Ah I mean the client will receive a buffer and not a base64 encoded string? I will test it out shortly.
@NghiaTranUIT commented on GitHub (Feb 20, 2021):
yes, Proxyman will automatically convert the base64String to a buffer when making a request/response. Therefore, the client will receive correctly
@SupremeTechnopriest commented on GitHub (Feb 20, 2021):
Beautiful! Take my money!
@NghiaTranUIT commented on GitHub (Feb 20, 2021):
If there is something that doesn't work as you expect. Feel free to give me an example code, I will check it out 😄
Meanwhile, I will update a Snippet Code Page for anyone who would use ArrayBuffer 👍
@SupremeTechnopriest commented on GitHub (Feb 20, 2021):
Sounds good! We can close this for now and if I run into any issues I will re open it. Thanks for your help!
@NghiaTranUIT commented on GitHub (Feb 20, 2021):
You're welcome 🎉
@NghiaTranUIT commented on GitHub (Feb 20, 2021):
Hey @SupremeTechnopriest
I've discovered that
btoadoesn't accept if the param is an ArrayBuffer.btoaonly accepts StringYou have to convert ArrayBuffer to String then encoding to Base64 like
var newBody = btoa(String.fromCharCode.apply(null, new Uint8Array(buffer)));Here is the example code:
Results
The response body has correct ArrayBuffer data
Improvements
In the next build, I will:
❌ Error: Could not parse the body as data. (Content-Type=application/octet-stream), so users can know how to fix itbtoa, it will automatically check ArrayBuffer type and do it behind the scene@NghiaTranUIT commented on GitHub (Feb 20, 2021):
Here is the beta build, which has some improvement I mention above 👍 (https://proxyman.s3.us-east-2.amazonaws.com/beta/Proxyman_2.18.0_Improve_Scripting_Array_Buffer.dmg)
You can use
btoato convert ArrayBuffer to Base64 without using the following helper code:Old
New
Notes
~/Library/Application Support/com.proxyman.NSProxy/addons/Base64.jsand relaunch the beta build.@SupremeTechnopriest commented on GitHub (Feb 22, 2021):
Thanks for the update. I will modify my custom addon to stringify the buffer until you bump the build version.
@SupremeTechnopriest commented on GitHub (Feb 23, 2021):
@NghiaTranUIT Quick question. How would I go the other way? Parsing an ArrayBuffer on the request. It comes in as Base64, but calling
atobon it turns it into a string.@SupremeTechnopriest commented on GitHub (Feb 23, 2021):
Nevermind, I figured it out.
Maybe you can add this to the Base64 addon as well 🙂
@NghiaTranUIT commented on GitHub (Feb 23, 2021):
Glad to know that you found the answer 😄 I will improve the atob too 👍