mirror of
https://github.com/ProxymanApp/Proxyman.git
synced 2026-04-26 08:35:53 +03:00
[GH-ISSUE #955] Script Error: Could not parse the body as Plain-Text. (Content-Type=text/html; charset=utf-8) #950
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#950
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 @phoenix5980 on GitHub (Aug 4, 2021).
Original GitHub issue: https://github.com/ProxymanApp/Proxyman/issues/955
Originally assigned to: @NghiaTranUIT on GitHub.
Proxyman version? (Ex. Proxyman 1.4.3)
Version 2.30.0
macOS Version? (Ex. mac 10.14)
11.4
Steps to reproduce
function onResponse(context, url, request, response) {
var bodyStr = response.body;
var decodeBody = atob(bodyStr);
var jsonBody = JSON.parse(decodeBody);
var counter = jsonBody["count"];
counter = counter + 1;
console.log(counter);
var jsonBody["count"] = counter;
var jsonStr= JSON.stringify(jsonBody);
var jsonEncode = btoa(jsonStr);
console.log(jsonEncode);
response.body = jsonEncode;
return response;
}
Expected behavior
In the last step, it should base64 encode the json string and send it to the client, but the log shows null for json.Encode and throws the following error:
❌Error: Could not parse the body as Plain-Text. (Content-Type=text/html; charset=utf-8)Screenshots (optional)
@NghiaTranUIT commented on GitHub (Aug 5, 2021):
hey @phoenix5980 look like your code is incorrect?
As you mention, jsonEncoded is
null. As a result, when you pass it toresponse.body, it causes the error.If you're going to use atob, or btoa, please include the addon. The reason is JavascriptCore from WebKit framework doesn't have built-in atob, btoa, window function. (Snippet code: https://docs.proxyman.io/scripting/snippet-code#use-base64-addon)
@phoenix5980 commented on GitHub (Aug 5, 2021):
Oh, sorry but I believe the problem wasn't that part. I did have the addon part but didn't included it in Steps to reproduce.
'// Decode Base64
const { atob } = require("@addons/Base64.js")
// Encode Base64
const { btoa } = require("@addons/Base64.js")'
I had it in the very beginning but forgot to paste it here. And I did use console.log to check decodeBody which was atob(bodyStr), and atob was definitely working. But btoa didn't work here. I'm not sure why.
@NghiaTranUIT commented on GitHub (Aug 6, 2021):
@phoenix5980 Can you log this variable?
I've run a test and
btoa()works fineLook like your syntax is incorrect.
It should be (remove the var)
@phoenix5980 commented on GitHub (Aug 6, 2021):
The actual log will be very long though. Please see attached.
Basically I'm trying to get the seed from the server, and make the seed plus 1 everytime client connected, then send the new seed to the client.
I used console.log() to check all parts of the script, the only problem is jsonEncode, which is Base64 Encoded jsonStr. While jsonStr, as you can see, is working. I wonder what happened to jsonEncode and why did it show 'null'.
@phoenix5980 commented on GitHub (Aug 16, 2021):
@NghiaTranUIT Hi, I used typeof() to check the type of jsonStr and jsonBody. Interestingly the type of jsonStr shows string, while jsonEncode, which is btoa(jsonStr), shows object.
@NghiaTranUIT commented on GitHub (Aug 16, 2021):
Hey @phoenix5980 I'm able to reproduce your issue. It looks like the jsonStr is too big, so
btoa()is failed somehow. I'm fixing it now 👍@NghiaTranUIT commented on GitHub (Aug 16, 2021):
Hey @phoenix5980 I discovered a bug in
btoa()function. It's failed if the input is large enough.Please use
base64Encode()function. It will work 👍Full code:
@phoenix5980 commented on GitHub (Aug 16, 2021):
Thanks, works like a charm.
@NghiaTranUIT commented on GitHub (Aug 16, 2021):
Fixed on this beta build: https://proxyman.s3.us-east-2.amazonaws.com/beta/Proxyman_2.31.0_Fix_btoa_bug.dmg
@phoenix5980 commented on GitHub (Nov 2, 2021):
I have to reopen the issue since I encountered another problem which probably relates to base64 issues or json parsing.
This script base64-decodes response from server into
decodedOLD, then parsedecodedOLDinto better json formatjsonBody, get/modify the seed injsonBody, then returnjsonBodyback todecodedNEW, then base64-encodesdecodedNEWback toresponse.body. However, even if I don't do any modification to the seed,decodedOLDis still different fromdecodedNEW, and the finalresponse.bodyis far different from the originalfromServer. I cannot locate the problem here, maybe another base64 issue or json parsing issue, my only guesses, cannot confirm.decompress in desktop.zip
@NghiaTranUIT commented on GitHub (Nov 2, 2021):
@phoenix5980 can you try this Base64 Decode/Encode?
instead of
atob,btoabecause from what I know, there is a little difference in terms of implementation.@phoenix5980 commented on GitHub (Nov 2, 2021):
The problem persists. I found that response.body wasn't encoded at all. Probably because the Base64 encoding process failed?
@phoenix5980 commented on GitHub (Nov 3, 2021):
@NghiaTranUIT
response.bodyis identical todecodedNEW, soresponse.body = btoa(decodedNEW);is not working. I tried base64Encode and the problem persists.@phoenix5980 commented on GitHub (Nov 21, 2021):
@NghiaTranUIT I tried several workarounds, no luck. Neither btoa() nor base64Encode() is working for decodedNEW. Not sure why.
output.zip
@NghiaTranUIT commented on GitHub (Nov 24, 2021):
@phoenix5980 I run your sample code and turn of it has an error when fetching your seed (See the Console Log)
Fix it
If I remove this code, the script run successfully.
Confirm that my request from Insomnia has base64 encoded string from
response.body = btoa(decodedNEW);Suggestion
@phoenix5980 can you remove the error code and test it again (Please open the console log)?