[GH-ISSUE #611] Scripting persist state between onRequest and onResponse #609

Open
opened 2026-03-03 19:20:19 +03:00 by kerem · 5 comments
Owner

Originally created by @edba on GitHub (Sep 1, 2020).
Original GitHub issue: https://github.com/ProxymanApp/Proxyman/issues/611

Originally assigned to: @NghiaTranUIT on GitHub.

In scripting it would be nice to be able to persist state between a request and its response, maybe via the context object or something similar. For example for holding on to the original requests url something like this could work:

function onRequest(context, url, request) {
  context.originalURL = url;
  request.schema = 'http';
  request.host = 'localhost';
  request.port = 8080;
  return request;
}


function onResponse(context, url, request, response) {
  // at this point `url` will be the updated one "localhost"
  // but context.originalURL would have the value of the original one.
  console.log(context.originalURL)
  return response;
}

this is a really simple example, but it would be useful to be able to reference state in onResponse that was derived inside onRequest

Originally created by @edba on GitHub (Sep 1, 2020). Original GitHub issue: https://github.com/ProxymanApp/Proxyman/issues/611 Originally assigned to: @NghiaTranUIT on GitHub. In scripting it would be nice to be able to persist state between a request and its response, maybe via the context object or something similar. For example for holding on to the original requests url something like this could work: ```javascript function onRequest(context, url, request) { context.originalURL = url; request.schema = 'http'; request.host = 'localhost'; request.port = 8080; return request; } function onResponse(context, url, request, response) { // at this point `url` will be the updated one "localhost" // but context.originalURL would have the value of the original one. console.log(context.originalURL) return response; } ``` this is a really simple example, but it would be useful to be able to reference state in `onResponse` that was derived inside `onRequest`
Author
Owner

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

That makes sense. I will find a way to support shared states between the request and response call 👍

<!-- gh-comment-id:684155947 --> @NghiaTranUIT commented on GitHub (Sep 1, 2020): That makes sense. I will find a way to support shared states between the request and response call 👍
Author
Owner

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

@edba let check out this build: https://download.proxyman.io/beta/Proxyman_2.5.3_Shared_State.dmg

You can use a global object sharedState to share the data between the onRequest/onResponse call. Please bear in mind that this state is reset for each script call 👍

function onRequest(context, url, request) {
  sharedState.url = url;
  sharedState.data = "custom";
  console.log(sharedState);
  return request;
}

function onResponse(context, url, request, response) {
   console.log(sharedState);
  
  // Inject and allow all origin
  response.headers["Access-Control-Allow-Origin"] = "*";

  // Done
  return response;
}
Screen Shot 2020-09-01 at 11 03 16
<!-- gh-comment-id:684185602 --> @NghiaTranUIT commented on GitHub (Sep 1, 2020): @edba let check out this build: https://download.proxyman.io/beta/Proxyman_2.5.3_Shared_State.dmg You can use a global object `sharedState` to share the data between the onRequest/onResponse call. Please bear in mind that this state is reset for each script call 👍 ```js function onRequest(context, url, request) { sharedState.url = url; sharedState.data = "custom"; console.log(sharedState); return request; } function onResponse(context, url, request, response) { console.log(sharedState); // Inject and allow all origin response.headers["Access-Control-Allow-Origin"] = "*"; // Done return response; } ``` <img width="2200" alt="Screen Shot 2020-09-01 at 11 03 16" src="https://user-images.githubusercontent.com/5878421/91793561-c0828f80-ec42-11ea-83c2-cd1fe34166ac.png">
Author
Owner

@edba commented on GitHub (Sep 1, 2020):

@NghiaTranUIT awesome! yeah this works 👍

<!-- gh-comment-id:684362374 --> @edba commented on GitHub (Sep 1, 2020): @NghiaTranUIT awesome! yeah this works 👍
Author
Owner

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

Glad to hear that work for you 🎉 😄

<!-- gh-comment-id:684480818 --> @NghiaTranUIT commented on GitHub (Sep 1, 2020): Glad to hear that work for you 🎉 😄
Author
Owner

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

I officially released the sharedState for the Scripting in Proxyman 2.6.0. Please check it out and the Doc: https://docs.proxyman.io/scripting/environment-variables

<!-- gh-comment-id:690382396 --> @NghiaTranUIT commented on GitHub (Sep 10, 2020): I officially released the sharedState for the Scripting in Proxyman 2.6.0. Please check it out and the Doc: https://docs.proxyman.io/scripting/environment-variables
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#609
No description provided.