[GH-ISSUE #1242] Scripting always logs a warning about missing onResoponse while it's there #1236

Closed
opened 2026-03-03 19:49:33 +03:00 by kerem · 9 comments
Owner

Originally created by @nikhil-thakkar on GitHub (May 20, 2022).
Original GitHub issue: https://github.com/ProxymanApp/Proxyman/issues/1242

Originally assigned to: @NghiaTranUIT on GitHub.

Proxyman version? (Ex. Proxyman 1.4.3)

3.5.0

macOS Version? (Ex. mac 10.14)

12.3.1

Steps to reproduce

  1. Create a new script file with the following contents
    `const file = require("@users/49933F43.order_status.json");

// Addons List: https://docs.proxyman.io/scripting/addons
const { sayHello } = require("@addons/HelloWorld.js");

/// This func is called if the Request Checkbox is Enabled. You can modify the Request Data here before the request hits to the server
/// e.g. Add/Update/Remove: host, scheme, port, path, headers, queries, comment, color and body (json, form, plain-text, base64 encoded string)
///
/// Action Menu -> Import File to import a JSON file and use in the script. Or Import directly with request.bodyFilePath = "~/Desktop/myfile.json" (Applied for Response too)
/// Use global object sharedState to share data between Requests/Response from different scripts (e.g. sharedState.data = "My-Data")
///
function onRequest(context, url, request) {
// console.log(request);
console.log(url);

// Update or Add new headers
// request.headers["X-New-Headers"] = "My-Value";

// Update or Add new queries
// request.queries["name"] = "Proxyman";

// Body
// var body = request.body;
// body["new-key"] = "new-value"
// request.body = body;

// Done
return request;
}

/// This func is called if the Response Checkbox is Enabled. You can modify the Response Data here before it goes to the client
/// e.g. Add/Update/Remove: headers, statusCode, comment, color and body (json, plain-text, base64 encoded string)
///
function onResponse(context, url, request, response) {
//console.log(response);

// Update or Add new headers
// response.headers["Content-Type"] = "application/json";

// Update status Code
// response.statusCode = 500;

// Update Body
// var body = response.body;
// body["new-key"] = "Proxyman";
// response.body = body;

// Or map a local file as a body
// response.bodyFilePath = "~/Desktop/myfile.json"

// Done
response.body = file;
response.statusCode = 200;
return response;
}`

  1. Try to repeat the request from proxyman so as to activate the script

Expected behavior

onResponse should be called

Screenshots (optional)

Screen Shot 2022-05-20 at 11 40 36
Originally created by @nikhil-thakkar on GitHub (May 20, 2022). Original GitHub issue: https://github.com/ProxymanApp/Proxyman/issues/1242 Originally assigned to: @NghiaTranUIT on GitHub. ### Proxyman version? (Ex. Proxyman 1.4.3) 3.5.0 ### macOS Version? (Ex. mac 10.14) 12.3.1 ### Steps to reproduce 1. Create a new script file with the following contents `const file = require("@users/49933F43.order_status.json"); // Addons List: https://docs.proxyman.io/scripting/addons const { sayHello } = require("@addons/HelloWorld.js"); /// This func is called if the Request Checkbox is Enabled. You can modify the Request Data here before the request hits to the server /// e.g. Add/Update/Remove: host, scheme, port, path, headers, queries, comment, color and body (json, form, plain-text, base64 encoded string) /// /// Action Menu -> Import File to import a JSON file and use in the script. Or Import directly with request.bodyFilePath = "~/Desktop/myfile.json" (Applied for Response too) /// Use global object `sharedState` to share data between Requests/Response from different scripts (e.g. sharedState.data = "My-Data") /// function onRequest(context, url, request) { // console.log(request); console.log(url); // Update or Add new headers // request.headers["X-New-Headers"] = "My-Value"; // Update or Add new queries // request.queries["name"] = "Proxyman"; // Body // var body = request.body; // body["new-key"] = "new-value" // request.body = body; // Done return request; } /// This func is called if the Response Checkbox is Enabled. You can modify the Response Data here before it goes to the client /// e.g. Add/Update/Remove: headers, statusCode, comment, color and body (json, plain-text, base64 encoded string) /// function onResponse(context, url, request, response) { //console.log(response); // Update or Add new headers // response.headers["Content-Type"] = "application/json"; // Update status Code // response.statusCode = 500; // Update Body // var body = response.body; // body["new-key"] = "Proxyman"; // response.body = body; // Or map a local file as a body // response.bodyFilePath = "~/Desktop/myfile.json" // Done response.body = file; response.statusCode = 200; return response; }` 3. Try to repeat the request from proxyman so as to activate the script ### Expected behavior onResponse should be called ### Screenshots (optional) <img width="419" alt="Screen Shot 2022-05-20 at 11 40 36" src="https://user-images.githubusercontent.com/3678113/169501036-dc62ef1b-0b74-4382-8e67-7486ba8983fa.png">
kerem 2026-03-03 19:49:33 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@NghiaTranUIT commented on GitHub (May 20, 2022):

It seems that the Response doesn't have the Content-Type: "application/json"; so Proxyman doesn't know how to parse the JSON Body.

@nikhil-thakkar please update with this code:

function onResponse(context, url, request, response) {

// Update or Add new headers
response.headers["Content-Type"] = "application/json";
response.body = file;
response.statusCode = 200;
return response;
}`
<!-- gh-comment-id:1132708541 --> @NghiaTranUIT commented on GitHub (May 20, 2022): It seems that the Response doesn't have the Content-Type: "application/json"; so Proxyman doesn't know how to parse the JSON Body. @nikhil-thakkar please update with this code: ```js function onResponse(context, url, request, response) { // Update or Add new headers response.headers["Content-Type"] = "application/json"; response.body = file; response.statusCode = 200; return response; }` ```
Author
Owner

@nikhil-thakkar commented on GitHub (May 20, 2022):

Thanks for the quick feedback. The content-type was there. I just commented it out for testing other scenarios. But unfortunately the issue persists :(

<!-- gh-comment-id:1132730907 --> @nikhil-thakkar commented on GitHub (May 20, 2022): Thanks for the quick feedback. The content-type was there. I just commented it out for testing other scenarios. But unfortunately the issue persists :(
Author
Owner

@NghiaTranUIT commented on GitHub (May 20, 2022):

It seems to be a bug. If you don't mind, please use this code:

Make sure you replace ~/Desktop/my_response.json"; with your JSON file.

function onResponse(context, url, request, response) {
 
  response.headers["Content-Type"] = "application/json";
  response.bodyFilePath = "~/Desktop/my_response.json";
 
  // Done
  return response;
}

and delete this line:

const file = require("@users/49933F43.order_status.json");

Ref: https://docs.proxyman.io/scripting/snippet-code#map-a-local-file-to-responses-body-like-map-local-tool-proxyman-2.25.0+

<!-- gh-comment-id:1132732364 --> @NghiaTranUIT commented on GitHub (May 20, 2022): It seems to be a bug. If you don't mind, please use this code: Make sure you replace `~/Desktop/my_response.json";` with your JSON file. ```js function onResponse(context, url, request, response) { response.headers["Content-Type"] = "application/json"; response.bodyFilePath = "~/Desktop/my_response.json"; // Done return response; } ``` and delete this line: ```js const file = require("@users/49933F43.order_status.json"); ``` Ref: https://docs.proxyman.io/scripting/snippet-code#map-a-local-file-to-responses-body-like-map-local-tool-proxyman-2.25.0+
Author
Owner

@NghiaTranUIT commented on GitHub (May 20, 2022):

I tested with the Import JSON file and it works fine.

-> Can you verify that your file `order_status.json is the correct JSON?

Screen Shot 2022-05-20 at 17 19 34
<!-- gh-comment-id:1132735545 --> @NghiaTranUIT commented on GitHub (May 20, 2022): I tested with the Import JSON file and it works fine. -> Can you verify that your file `order_status.json is the correct JSON? <img width="1796" alt="Screen Shot 2022-05-20 at 17 19 34" src="https://user-images.githubusercontent.com/5878421/169507890-15d9df29-ac0c-4bcf-a3b7-97de83b66de2.png">
Author
Owner

@nikhil-thakkar commented on GitHub (May 20, 2022):

@NghiaTranUIT Thanks again for helping me on this one. Here is the screenshot of where the json is stored and here are the contents
{ "id": "kfj90", "status": "Pending" }
Screen Shot 2022-05-20 at 12 54 40

Screen Shot 2022-05-20 at 13 44 26
<!-- gh-comment-id:1132764907 --> @nikhil-thakkar commented on GitHub (May 20, 2022): @NghiaTranUIT Thanks again for helping me on this one. Here is the screenshot of where the json is stored and here are the contents `{ "id": "kfj90", "status": "Pending" }` <img width="458" alt="Screen Shot 2022-05-20 at 12 54 40" src="https://user-images.githubusercontent.com/3678113/169513853-fcadc0a5-b105-475a-af92-0d174254104c.png"> <img width="1240" alt="Screen Shot 2022-05-20 at 13 44 26" src="https://user-images.githubusercontent.com/3678113/169521663-78135f13-3762-4d59-99ab-5c0785ed7535.png">
Author
Owner

@NghiaTranUIT commented on GitHub (May 21, 2022):

@nikhil-thakkar let's try this build: https://proxyman.s3.us-east-2.amazonaws.com/beta/Proxyman_3.5.0_fix_script_undefined.dmg

I fixed a small bug, which causes the TypeError: undefined is not a function 👍

<!-- gh-comment-id:1133489851 --> @NghiaTranUIT commented on GitHub (May 21, 2022): @nikhil-thakkar let's try this build: https://proxyman.s3.us-east-2.amazonaws.com/beta/Proxyman_3.5.0_fix_script_undefined.dmg I fixed a small bug, which causes the `TypeError: undefined is not a function` 👍
Author
Owner

@nikhil-thakkar commented on GitHub (May 21, 2022):

Thanks for the quick fix @NghiaTranUIT . I keep both the versions when installing the one you provided. And tried both. Unfortunately the issue still persists.
Not sure what's going wrong.
Can you point me in Proxyman internal logs so that I can take a look at the code to understand whether it's my fault?

<!-- gh-comment-id:1133580634 --> @nikhil-thakkar commented on GitHub (May 21, 2022): Thanks for the quick fix @NghiaTranUIT . I keep both the versions when installing the one you provided. And tried both. Unfortunately the issue still persists. Not sure what's going wrong. Can you point me in Proxyman internal logs so that I can take a look at the code to understand whether it's my fault?
Author
Owner

@NghiaTranUIT commented on GitHub (May 23, 2022):

@nikhil-thakkar I suppose that your original request has some problem. If you don't mind, please export the request (without running via the Script) and share with me at support@proxyman.io

I'd like to check whether the Body is matched with Request/Response Header Content-Type or not.

<!-- gh-comment-id:1134048249 --> @NghiaTranUIT commented on GitHub (May 23, 2022): @nikhil-thakkar I suppose that your original request has some problem. If you don't mind, please export the request (without running via the Script) and share with me at support@proxyman.io I'd like to check whether the Body is matched with Request/Response Header Content-Type or not.
Author
Owner

@nikhil-thakkar commented on GitHub (Jun 15, 2022):

@NghiaTranUIT Sorry for the delayed response. For the time being I have put this on back burner and looking into other things. I will re-open the ticket if the issue persists and/or share the request with you.

<!-- gh-comment-id:1156034450 --> @nikhil-thakkar commented on GitHub (Jun 15, 2022): @NghiaTranUIT Sorry for the delayed response. For the time being I have put this on back burner and looking into other things. I will re-open the ticket if the issue persists and/or share the request with you.
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#1236
No description provided.