[GH-ISSUE #615] request.body in scripting is incorrect #612

Closed
opened 2026-03-03 19:20:21 +03:00 by kerem · 6 comments
Owner

Originally created by @trycatchx on GitHub (Sep 4, 2020).
Original GitHub issue: https://github.com/ProxymanApp/Proxyman/issues/615

Proxyman version? (Ex. Proxyman 1.4.3)

2.5.3

1、The http.body of my request on the homepage is like this.
image
{"req_0":{"method":"GetNewCommentList","module":"music.globalComment.CommentReadServer","param":{"BizType":31,"BizId":"012wNuzn25fa0b","LastCommentId":"","LastCommentSeqNo":"","PageSize":15,"PageNum":0,"FromCommentId":"","WithHot":1}},"comm":{"g_tk":5381,"uin":0,"format":"json","platform":"h5","ct":23,"cv":0}}

2、The same http.body is displayed differently in scripting:
image

{
  "{\"req_0\":{\"method\":\"GetNewCommentList\",\"module\":\"music.globalComment.CommentReadServer\",\"param\":{\"BizType\":31,\"BizId\":\"012wNuzn25fa0b\",\"LastCommentId\":\"\",\"LastCommentSeqNo\":\"\",\"PageSize\":15,\"PageNum\":0,\"FromCommentId\":\"\",\"WithHot\":1}},\"comm\":{\"g_tk\":5381,\"uin\":0,\"format\":\"json\",\"platform\":\"h5\",\"ct\":23,\"cv\":0}}": ""
}

I am use this code to print it:

function onRequest(context, url, request) {
  // add header
  var jsonBody = request.body;
  console.log(formatJSON(JSON.stringify(jsonBody)))
  
  return request
}

The requset.body in scripting is wrong ? which makes me unable to modify requset.body.

@NghiaTranUIT Can you help me solve it?

Originally created by @trycatchx on GitHub (Sep 4, 2020). Original GitHub issue: https://github.com/ProxymanApp/Proxyman/issues/615 ### Proxyman version? (Ex. Proxyman 1.4.3) 2.5.3 1、The http.body of my request on the homepage is like this. ![image](https://user-images.githubusercontent.com/6050250/92217500-5aa83900-eeca-11ea-9868-7556a255274b.png) `{"req_0":{"method":"GetNewCommentList","module":"music.globalComment.CommentReadServer","param":{"BizType":31,"BizId":"012wNuzn25fa0b","LastCommentId":"","LastCommentSeqNo":"","PageSize":15,"PageNum":0,"FromCommentId":"","WithHot":1}},"comm":{"g_tk":5381,"uin":0,"format":"json","platform":"h5","ct":23,"cv":0}}` 2、The same http.body is displayed differently in scripting: ![image](https://user-images.githubusercontent.com/6050250/92217726-a955d300-eeca-11ea-9200-a4c8b4614ac4.png) ``` { "{\"req_0\":{\"method\":\"GetNewCommentList\",\"module\":\"music.globalComment.CommentReadServer\",\"param\":{\"BizType\":31,\"BizId\":\"012wNuzn25fa0b\",\"LastCommentId\":\"\",\"LastCommentSeqNo\":\"\",\"PageSize\":15,\"PageNum\":0,\"FromCommentId\":\"\",\"WithHot\":1}},\"comm\":{\"g_tk\":5381,\"uin\":0,\"format\":\"json\",\"platform\":\"h5\",\"ct\":23,\"cv\":0}}": "" } ``` I am use this code to print it: ``` function onRequest(context, url, request) { // add header var jsonBody = request.body; console.log(formatJSON(JSON.stringify(jsonBody))) return request } ``` The requset.body in scripting is wrong ? which makes me unable to modify requset.body. @NghiaTranUIT Can you help me solve it?
kerem 2026-03-03 19:20:21 +03:00
Author
Owner

@NghiaTranUIT commented on GitHub (Sep 4, 2020):

@zhangchaojiong Since the ContentType of the Request is form-urlencoded, so Proxyman attempts to parse the body as a Form. Thus, you get the weird body.

There are three ways to fix it:

  1. If you're a developer of this app, please try to change the Content Type of the Request from your app. It should be application/json. Then, request.body is a JSON Object

  2. Or You can fix in the script by using

function onRequest(context, url, request) {
    // Change the Content Type to JSON
    request.headers["Content-Type"] = "application/json"

    // Get json from the weird body
    var firstKey = Object.keys(request.body)[0];
    var jsonBody = JSON.parse(firstKey)
    // Update some key
    jsonBody["new-key"] = "Proxyman"

   // Assign again
   request.body = jsonBody;
   return request;
}
  1. I will try to guess and parse body properly or find the way to return raw Body without parsing in the next build 👍
<!-- gh-comment-id:687010813 --> @NghiaTranUIT commented on GitHub (Sep 4, 2020): @zhangchaojiong Since the ContentType of the Request is `form-urlencoded`, so Proxyman attempts to parse the body as a Form. Thus, you get the weird body. There are three ways to fix it: 1. If you're a developer of this app, please try to change the Content Type of the Request from your app. It should be `application/json`. Then, `request.body` is a JSON Object 2. Or You can fix in the script by using ```js function onRequest(context, url, request) { // Change the Content Type to JSON request.headers["Content-Type"] = "application/json" // Get json from the weird body var firstKey = Object.keys(request.body)[0]; var jsonBody = JSON.parse(firstKey) // Update some key jsonBody["new-key"] = "Proxyman" // Assign again request.body = jsonBody; return request; } ``` 3. I will try to guess and parse body properly or find the way to return raw Body without parsing in the next build 👍
Author
Owner

@trycatchx commented on GitHub (Sep 4, 2020):

@NghiaTranUIT 👍 you are right !!thx !

<!-- gh-comment-id:687019105 --> @trycatchx commented on GitHub (Sep 4, 2020): @NghiaTranUIT 👍 you are right !!thx !
Author
Owner

@NghiaTranUIT commented on GitHub (Sep 4, 2020):

In the next build, I will add rawBody into request and response. Thus, you can get the raw body without depending on Content-Type, then you can parse whatever you like 👍 😄

<!-- gh-comment-id:687019719 --> @NghiaTranUIT commented on GitHub (Sep 4, 2020): In the next build, I will add `rawBody` into `request` and `response`. Thus, you can get the raw body without depending on Content-Type, then you can parse whatever you like 👍 😄
Author
Owner

@trycatchx commented on GitHub (Sep 4, 2020):

@NghiaTranUIT Looking forward to next version!

<!-- gh-comment-id:687020611 --> @trycatchx commented on GitHub (Sep 4, 2020): @NghiaTranUIT Looking forward to next version!
Author
Owner

@NghiaTranUIT commented on GitHub (Sep 10, 2020):

Good news that I supported rawBody in the request and response objects @zhangchaojiong

It's a raw String or Base64 encoded String (for binary data), then you can manually parse it 👍

https://docs.proxyman.io/scripting/script

<!-- gh-comment-id:690384673 --> @NghiaTranUIT commented on GitHub (Sep 10, 2020): Good news that I supported `rawBody` in the **request** and **response** objects @zhangchaojiong It's a raw String or Base64 encoded String (for binary data), then you can manually parse it 👍 https://docs.proxyman.io/scripting/script
Author
Owner

@trycatchx commented on GitHub (Sep 22, 2020):

@NghiaTranUIT Happy to hear it!! 👍

<!-- gh-comment-id:696578630 --> @trycatchx commented on GitHub (Sep 22, 2020): @NghiaTranUIT Happy to hear it!! 👍
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/Proxyman#612
No description provided.