[GH-ISSUE #1193] How to send HTTP requests in onResponse in scripting Menu #1189

Open
opened 2026-03-03 19:49:08 +03:00 by kerem · 17 comments
Owner

Originally created by @1414044032 on GitHub (Mar 30, 2022).
Original GitHub issue: https://github.com/ProxymanApp/Proxyman/issues/1193

Originally assigned to: @NghiaTranUIT on GitHub.

Proxyman version? (Ex. Proxyman 1.4.3)

Proxyman 1.4.3

macOS Version? (Ex. mac 10.14)

mac 12.2.1

Steps to reproduce

Writing your own scripts for Scripting

Expected behavior

Proxyman provides Base64 lodash and other scripts.
But I want to send the network request through the Node HTTP module in the onResponse function.
I found that I couldn't use any of the built-in modules in Node. It prints undefined。
I want to process the data in onResponse and send it to an address via an HTTP request。

Screenshots (optional)

Originally created by @1414044032 on GitHub (Mar 30, 2022). Original GitHub issue: https://github.com/ProxymanApp/Proxyman/issues/1193 Originally assigned to: @NghiaTranUIT on GitHub. ### Proxyman version? (Ex. Proxyman 1.4.3) Proxyman 1.4.3 ### macOS Version? (Ex. mac 10.14) mac 12.2.1 ### Steps to reproduce Writing your own scripts for Scripting ### Expected behavior Proxyman provides Base64 lodash and other scripts. But I want to send the network request through the Node HTTP module in the onResponse function. I found that I couldn't use any of the built-in modules in Node. It prints undefined。 I want to process the data in onResponse and send it to an address via an HTTP request。 ### Screenshots (optional)
Author
Owner

@NghiaTranUIT commented on GitHub (Mar 30, 2022):

Currently, it's not possible to make an async API call in the onResponse() because it's a sync function.

Can you elaborate on what network request you'd like to use? I might bring axios or fetch if it's possible

<!-- gh-comment-id:1082869813 --> @NghiaTranUIT commented on GitHub (Mar 30, 2022): Currently, it's not possible to make an async API call in the `onResponse()` because it's a sync function. Can you elaborate on what network request you'd like to use? I might bring `axios` or `fetch` if it's possible
Author
Owner

@1414044032 commented on GitHub (Mar 30, 2022):

It would be best to support Axios to send requests.
I don't really care if the request succeeds, I just want to push some data parsed in onResponse to my webHook address.
For example:
OnResponse might parse out some of the data, which I can push to a Web address on my own server via axios.
As for whether the request was successfully sent to my server, whether my server returned a status code of 200 is actually not my concern.
I think this feature will be great because there are limitations to what developers can do in Scripting right now.
----The translation is from Youdao Dictionary

<!-- gh-comment-id:1082890539 --> @1414044032 commented on GitHub (Mar 30, 2022): It would be best to support Axios to send requests. I don't really care if the request succeeds, I just want to push some data parsed in onResponse to my webHook address. For example: OnResponse might parse out some of the data, which I can push to a Web address on my own server via axios. As for whether the request was successfully sent to my server, whether my server returned a status code of 200 is actually not my concern. I think this feature will be great because there are limitations to what developers can do in Scripting right now. ----The translation is from Youdao Dictionary
Author
Owner

@michael-elkabetz commented on GitHub (Apr 6, 2022):

i found my self need this ability also @NghiaTranUIT, once received response i want to send an http request using Axios/http/curl to my server to get slack notification sync/async its irrelevant for me either.

@NghiaTranUIT currently there is any way to do it?

<!-- gh-comment-id:1090716628 --> @michael-elkabetz commented on GitHub (Apr 6, 2022): i found my self need this ability also @NghiaTranUIT, once received response i want to send an http request using Axios/http/curl to my server to get slack notification sync/async its irrelevant for me either. @NghiaTranUIT currently there is any way to do it?
Author
Owner

@NghiaTranUIT commented on GitHub (Apr 7, 2022):

Unfortunately, there is no way to perform an Async call in the scripting at the moment.

The JS Context is going to release as soon as the script is called.

I'm going to support Async soon 👍

<!-- gh-comment-id:1090986022 --> @NghiaTranUIT commented on GitHub (Apr 7, 2022): Unfortunately, there is no way to perform an Async call in the scripting at the moment. The JS Context is going to release as soon as the script is called. I'm going to support Async soon 👍
Author
Owner

@michael-elkabetz commented on GitHub (Apr 19, 2022):

Hey @1414044032, @NghiaTranUIT,

I tried to add this ability by myself and unfortunately failed, hope that you will be able to assist.

I think i have tried any axios.js version out there, here are my finding.

The best result i achieved was with axios version 0.14 and 0.13.
I got my console.log message printed inside my axion.get('..').than, but it seems like the request was not actually executed. the data i had inside was:

{"transformRequest":{},"transformResponse":{},"headers":{"Accept":"application/json, text/plain, */*"},"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"method":"get","url":"http://webcode.me"}

In any version 0.15 and above i got the following error:
adapter is not a function. (In 'adapter(config)', 'adapter' is undefined)

In any version older than 0.13 i didn't receive an error however nothing was printed or triggered.

below you can find my code snippet:
`const axios = require('@libs/axios.js');

function onRequest(context, url, request) {

return request;
}

function onResponse(context, url, request, response) {
response.headers["Content-Type"] = "application/json";

axios.get("http://webcode.me")
.then(function(response) {
console.log("data: " + JSON.stringify(response));
})
.catch(function(error) {
console.log("error:" + error);
})
return response;
}`

<!-- gh-comment-id:1103002277 --> @michael-elkabetz commented on GitHub (Apr 19, 2022): Hey @1414044032, @NghiaTranUIT, I tried to add this ability by myself and unfortunately failed, hope that you will be able to assist. I think i have tried any axios.js version out there, here are my finding. The best result i achieved was with axios version 0.14 and 0.13. I got my console.log message printed inside my axion.get('..').than, but it seems like the request was not actually executed. the data i had inside was: `{"transformRequest":{},"transformResponse":{},"headers":{"Accept":"application/json, text/plain, */*"},"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"method":"get","url":"http://webcode.me"}` In any version 0.15 and above i got the following error: `adapter is not a function. (In 'adapter(config)', 'adapter' is undefined)` In any version older than 0.13 i didn't receive an error however nothing was printed or triggered. below you can find my code snippet: `const axios = require('@libs/axios.js'); function onRequest(context, url, request) { return request; } function onResponse(context, url, request, response) { response.headers["Content-Type"] = "application/json"; axios.get("http://webcode.me") .then(function(response) { console.log("data: " + JSON.stringify(response)); }) .catch(function(error) { console.log("error:" + error); }) return response; }`
Author
Owner

@1414044032 commented on GitHub (Apr 20, 2022):

Hey @1414044032, @NghiaTranUIT,

I tried to add this ability by myself and unfortunately failed, hope that you will be able to assist.

I think i have tried any axios.js version out there, here are my finding.

The best result i achieved was with axios version 0.14 and 0.13. I got my console.log message printed inside my axion.get('..').than, but it seems like the request was not actually executed. the data i had inside was:

{"transformRequest":{},"transformResponse":{},"headers":{"Accept":"application/json, text/plain, */*"},"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"method":"get","url":"http://webcode.me"}

In any version 0.15 and above i got the following error: adapter is not a function. (In 'adapter(config)', 'adapter' is undefined)

In any version older than 0.13 i didn't receive an error however nothing was printed or triggered.

below you can find my code snippet: `const axios = require('@libs/axios.js');

function onRequest(context, url, request) {

return request; }

function onResponse(context, url, request, response) { response.headers["Content-Type"] = "application/json";

axios.get("http://webcode.me") .then(function(response) { console.log("data: " + JSON.stringify(response)); }) .catch(function(error) { console.log("error:" + error); }) return response; }`

I think add this function only by author. Axios in node env will use HTTP module to send request. But in this application NODE env is broken. That's my guess.

<!-- gh-comment-id:1103348931 --> @1414044032 commented on GitHub (Apr 20, 2022): > Hey @1414044032, @NghiaTranUIT, > > I tried to add this ability by myself and unfortunately failed, hope that you will be able to assist. > > I think i have tried any axios.js version out there, here are my finding. > > The best result i achieved was with axios version 0.14 and 0.13. I got my console.log message printed inside my axion.get('..').than, but it seems like the request was not actually executed. the data i had inside was: > > `{"transformRequest":{},"transformResponse":{},"headers":{"Accept":"application/json, text/plain, */*"},"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"method":"get","url":"http://webcode.me"}` > > In any version 0.15 and above i got the following error: `adapter is not a function. (In 'adapter(config)', 'adapter' is undefined)` > > In any version older than 0.13 i didn't receive an error however nothing was printed or triggered. > > below you can find my code snippet: `const axios = require('@libs/axios.js'); > > function onRequest(context, url, request) { > > return request; } > > function onResponse(context, url, request, response) { response.headers["Content-Type"] = "application/json"; > > axios.get("http://webcode.me") .then(function(response) { console.log("data: " + JSON.stringify(response)); }) .catch(function(error) { console.log("error:" + error); }) return response; }` I think add this function only by author. Axios in node env will use HTTP module to send request. But in this application NODE env is broken. That's my guess.
Author
Owner

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

Hey guys. I'm working on this feature by inspiring from the Scriptable app.

See this Request: https://docs.scriptable.app/request/#-loadjson

Basically, we can

var req = new Request(url);
var json = await req.loadJSON();
<!-- gh-comment-id:1103699656 --> @NghiaTranUIT commented on GitHub (Apr 20, 2022): Hey guys. I'm working on this feature by inspiring from the Scriptable app. See this Request: https://docs.scriptable.app/request/#-loadjson Basically, we can ```js var req = new Request(url); var json = await req.loadJSON(); ```
Author
Owner

@michael-elkabetz commented on GitHub (Apr 21, 2022):

Hey guys. I'm working on this feature by inspiring from the Scriptable app.

See this Request: https://docs.scriptable.app/request/#-loadjson

Basically, we can

var req = new Request(url);
var json = await req.loadJSON();

You mean that once the feature will be ready this is the way we will be able to execute requests?

<!-- gh-comment-id:1104698796 --> @michael-elkabetz commented on GitHub (Apr 21, 2022): > Hey guys. I'm working on this feature by inspiring from the Scriptable app. > > See this Request: https://docs.scriptable.app/request/#-loadjson > > Basically, we can > > ```js > var req = new Request(url); > var json = await req.loadJSON(); > ``` You mean that once the feature will be ready this is the way we will be able to execute requests?
Author
Owner

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

@whyike yes. You can consume Restful APIs 👍

<!-- gh-comment-id:1104702361 --> @NghiaTranUIT commented on GitHub (Apr 21, 2022): @whyike yes. You can consume Restful APIs 👍
Author
Owner

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

The good news is that I finally found a solution that allows you to consume a Restful API 😄

var body = {
  "name": "Proxyman"
}
var result = await $http.post("https://httpbin.org", {
  "headers": {
    "Content-Type": "application/json",
    "X-Key": "My Key"
  },
  "body": body
})

console.log(result)

I'm working on it, and will soon send you guys a Beta build soon 🙌

<!-- gh-comment-id:1114172836 --> @NghiaTranUIT commented on GitHub (May 1, 2022): The good news is that I finally found a solution that allows you to consume a Restful API 😄 ```js var body = { "name": "Proxyman" } var result = await $http.post("https://httpbin.org", { "headers": { "Content-Type": "application/json", "X-Key": "My Key" }, "body": body }) console.log(result) ``` -------------- I'm working on it, and will soon send you guys a Beta build soon 🙌
Author
Owner

@michael-elkabetz commented on GitHub (May 1, 2022):

@NghiaTranUIT you are truly amazing dear!!!! waiting for that!!!!

<!-- gh-comment-id:1114174975 --> @michael-elkabetz commented on GitHub (May 1, 2022): @NghiaTranUIT you are truly amazing dear!!!! waiting for that!!!!
Author
Owner

@michael-elkabetz commented on GitHub (May 12, 2022):

Any updates dear @NghiaTranUIT?

<!-- gh-comment-id:1124671609 --> @michael-elkabetz commented on GitHub (May 12, 2022): Any updates dear @NghiaTranUIT?
Author
Owner

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

@whyike @1414044032 you might this Beta build: https://proxyman.s3.us-east-2.amazonaws.com/beta/Proxyman_3.4.0_Async_HTTP_Request_on_Scripting.dmg

Note

  • It's not finished yet, you might encounter some bugs. Please let me know.
  • Use async onRequest() or async onResponse()
  • Use await $http.get() / await $http.post() / await $http.delete() / await $http.update()
  • Make sure you have correct Content-Type for the Body. Use "Content-Type": "application/json" for JSON Body.

Sampe code

async function onResponse(context, url, request, response) {
  // console.log(response);

  var param = {
    body: {
      "user": {
        "name": "Proxyman"
      }
    },
    headers: {
      "Content-Type": "application/json"
    }
  }

  var output = await $http.post("https://httpbin.org/post?id=123", param);
  console.log(output);
  console.log(output.body)
  console.log(output.headers)

  // Done
  return response;
}

Screenshots

Screen Shot 2022-05-12 at 22 20 06
<!-- gh-comment-id:1125132029 --> @NghiaTranUIT commented on GitHub (May 12, 2022): @whyike @1414044032 you might this Beta build: https://proxyman.s3.us-east-2.amazonaws.com/beta/Proxyman_3.4.0_Async_HTTP_Request_on_Scripting.dmg ### Note - It's not finished yet, you might encounter some bugs. Please let me know. - Use `async onRequest()` or `async onResponse()` - Use `await $http.get()` / `await $http.post()` / `await $http.delete()` / `await $http.update()` - Make sure you have correct Content-Type for the Body. Use `"Content-Type": "application/json"` for JSON Body. ### Sampe code ```js async function onResponse(context, url, request, response) { // console.log(response); var param = { body: { "user": { "name": "Proxyman" } }, headers: { "Content-Type": "application/json" } } var output = await $http.post("https://httpbin.org/post?id=123", param); console.log(output); console.log(output.body) console.log(output.headers) // Done return response; } ``` ### Screenshots <img width="1715" alt="Screen Shot 2022-05-12 at 22 20 06" src="https://user-images.githubusercontent.com/5878421/168110238-1f3ad511-b956-4df2-93b3-74dfb091cbb7.png">
Author
Owner

@michael-elkabetz commented on GitHub (May 12, 2022):

Amazing! @NghiaTranUIT, testing it right away!

<!-- gh-comment-id:1125228849 --> @michael-elkabetz commented on GitHub (May 12, 2022): Amazing! @NghiaTranUIT, testing it right away!
Author
Owner

@michael-elkabetz commented on GitHub (May 12, 2022):

It is working amazing!!!

Thanks a lot!!!!! @NghiaTranUIT

<!-- gh-comment-id:1125256256 --> @michael-elkabetz commented on GitHub (May 12, 2022): It is working amazing!!! Thanks a lot!!!!! @NghiaTranUIT
Author
Owner
<!-- gh-comment-id:1125784470 --> @NghiaTranUIT commented on GitHub (May 13, 2022): Here is the final Beta buid: https://proxyman.s3.us-east-2.amazonaws.com/beta/Proxyman_3.4.0_Support_Async_$http.get.dmg - Document: https://docs.proxyman.io/scripting/async-await-request - Snippet Code: https://docs.proxyman.io/scripting/snippet-code#make-async-await-http-request
Author
Owner

@1414044032 commented on GitHub (May 22, 2022):

It's such a great feature。I gave it a try and it met my needs very well. Thanks to the authors for their development work

<!-- gh-comment-id:1133906174 --> @1414044032 commented on GitHub (May 22, 2022): It's such a great feature。I gave it a try and it met my needs very well. Thanks to the authors for their development work
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#1189
No description provided.