[GH-ISSUE #1466] [Scripting] - onResponse Delay #1457

Open
opened 2026-03-03 19:51:41 +03:00 by kerem · 7 comments
Owner

Originally created by @michael-elkabetz on GitHub (Dec 12, 2022).
Original GitHub issue: https://github.com/ProxymanApp/Proxyman/issues/1466

Description

We have noticed that "onResponse" method is being called after short delay when using local proxy server.
There are any performance improvements planned for the upcoming future?

Thanks a lot for an amazing product!
Mike.

Originally created by @michael-elkabetz on GitHub (Dec 12, 2022). Original GitHub issue: https://github.com/ProxymanApp/Proxyman/issues/1466 ## Description We have noticed that "onResponse" method is being called after short delay when using local proxy server. There are any performance improvements planned for the upcoming future? Thanks a lot for an amazing product! Mike.
Author
Owner

@NghiaTranUIT commented on GitHub (Dec 13, 2022):

Hey @whyike, if you notice the delay in the onResponse, can you share with me a few information:

  • Do you use await async ? If you only use sync func, you might remove the async.
    For example:
async function onResponse() {}

to

function onResponse() {}
  • Is the logic in the onResponse function heavy-computation?
<!-- gh-comment-id:1347613114 --> @NghiaTranUIT commented on GitHub (Dec 13, 2022): Hey @whyike, if you notice the delay in the `onResponse`, can you share with me a few information: - Do you use `await async` ? If you only use sync func, you might remove the `async`. For example: ```js async function onResponse() {} ``` to ``` function onResponse() {} ``` - Is the logic in the `onResponse` function heavy-computation?
Author
Owner

@michael-elkabetz commented on GitHub (Dec 13, 2022):

Hey @NghiaTranUIT,

First of all, thanks for the prompt response!!! really appreciate it!

No heavy-computation, however, await is mandatory because i'm getting a JSON from the response, extract some data and i'm triggering another REST call using $http.put(URL, param);.

Any other suggestions how can i improve or optimize the operation are welcome :).

Thank you champ!
Mike.

<!-- gh-comment-id:1347771675 --> @michael-elkabetz commented on GitHub (Dec 13, 2022): Hey @NghiaTranUIT, First of all, thanks for the prompt response!!! really appreciate it! No heavy-computation, however, `await` is mandatory because i'm getting a JSON from the response, extract some data and i'm triggering another REST call using `$http.put(URL, param);`. Any other suggestions how can i improve or optimize the operation are welcome :). Thank you champ! Mike.
Author
Owner

@NghiaTranUIT commented on GitHub (Dec 13, 2022):

So it's a reason, you make a REST call, so it takes time.

To improve the performance, please improve your endpoint, so it returns the response faster.

In terms of Proxyman app, we could not do it since it depends on the external networking calls, which is your API.


If the REST API response doesn't change, you might cache it on your Desktop, then read the file.

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

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:1347825872 --> @NghiaTranUIT commented on GitHub (Dec 13, 2022): So it's a reason, you make a REST call, so it takes time. To improve the performance, please improve your endpoint, so it returns the response faster. In terms of Proxyman app, we could not do it since it depends on the external networking calls, which is your API. ------------ If the REST API response doesn't change, you might cache it on your Desktop, then read the file. ```js function onResponse(context, url, request, response) { response.headers["Content-Type"] = "application/json"; response.bodyFilePath = "~/Desktop/my_response.json"; // Done return response; } ``` 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

@michael-elkabetz commented on GitHub (Dec 13, 2022):

Thanks again for the quick response, you are truly amazing!

Just to make sure, are you suggesting to save the response on disk and read it from disk afterwords, is it will be quicker than read directly from the response object?

here is my snippet, just to be aligned.

async function onResponse(context, url, request, response) {
if (response.body.data.length > 0) {
let data = response.body.data[0];
let valueThatINeed1 = data.value1;
let valueThatINeed2 = data.value2;
let URL = "https://example.com/" + valueThatINeed1;
var param = {
body: {
"newData": valueThatINeed2
},
headers: {
"Accept": '/',
"Accept-Encoding": 'gzip, deflate, br',
"Content-Type": 'application/json'
}
}
var output = await $http.put(URL, param);
if (output.statusCode === 200) {
console.log('updated successfully');
}
}
return response;
}

Thanks,
Mike

<!-- gh-comment-id:1347856249 --> @michael-elkabetz commented on GitHub (Dec 13, 2022): Thanks again for the quick response, you are truly amazing! Just to make sure, are you suggesting to save the response on disk and read it from disk afterwords, is it will be quicker than read directly from the response object? here is my snippet, just to be aligned. > async function onResponse(context, url, request, response) { > if (response.body.data.length > 0) { > let data = response.body.data[0]; > let valueThatINeed1 = data.value1; > let valueThatINeed2 = data.value2; > let URL = "https://example.com/" + valueThatINeed1; > var param = { > body: { > "newData": valueThatINeed2 > }, > headers: { > "Accept": '*/*', > "Accept-Encoding": 'gzip, deflate, br', > "Content-Type": 'application/json' > } > } > var output = await $http.put(URL, param); > if (output.statusCode === 200) { > console.log('updated successfully'); > } > } > return response; > } Thanks, Mike
Author
Owner

@NghiaTranUIT commented on GitHub (Dec 13, 2022):

Yes, it's definitely faster.

The problem is this line:

var output = await $http.put(URL, param);

It will call an external network, which is slow.

By not using this line, and read from the file, it will improve the performance 👍

Here is the step:

  1. Open Safari or cURL and make a Rest call
  2. Save a response to your Desktop
  3. Open the Scripting and use the following code
function onResponse(context, url, request, response) {
 
  response.headers["Content-Type"] = "application/json";
  response.bodyFilePath = "~/Desktop/my_response.json";
 
  // Done
  return response;
}
<!-- gh-comment-id:1347874661 --> @NghiaTranUIT commented on GitHub (Dec 13, 2022): Yes, it's definitely faster. The problem is this line: ``` var output = await $http.put(URL, param); ``` It will call an external network, which is slow. By not using this line, and read from the file, it will improve the performance 👍 Here is the step: 1. Open Safari or cURL and make a Rest call 2. Save a response to your Desktop 3. Open the Scripting and use the following code ```js function onResponse(context, url, request, response) { response.headers["Content-Type"] = "application/json"; response.bodyFilePath = "~/Desktop/my_response.json"; // Done return response; } ```
Author
Owner

@michael-elkabetz commented on GitHub (Dec 13, 2022):

Got it dear, however we can't read the response for disk because we have to update an external service when onResponse called.

so there are no more performance improvements that we can add here right?

<!-- gh-comment-id:1347974077 --> @michael-elkabetz commented on GitHub (Dec 13, 2022): Got it dear, however we can't read the response for disk because we have to update an external service when onResponse called. so there are no more performance improvements that we can add here right?
Author
Owner

@NghiaTranUIT commented on GitHub (Dec 13, 2022):

So, there is only 1 solution. Make your Backend faster, so the REST API will be faster.

<!-- gh-comment-id:1347977700 --> @NghiaTranUIT commented on GitHub (Dec 13, 2022): So, there is only 1 solution. Make your Backend faster, so the REST API will be faster.
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#1457
No description provided.