[GH-ISSUE #852] request.scheme or request.schema? #847

Closed
opened 2026-03-03 19:22:22 +03:00 by kerem · 14 comments
Owner

Originally created by @chaoyangnz on GitHub (Apr 8, 2021).
Original GitHub issue: https://github.com/ProxymanApp/Proxyman/issues/852

Originally assigned to: @NghiaTranUIT on GitHub.

Proxyman version? (Ex. Proxyman 1.4.3)

latest

macOS Version? (Ex. mac 10.14)

10.15

Steps to reproduce

I am using scripting tool to add custom script, but i found the documentation saying request.schema. Normally we use scheme to refer to http or https. is that a typo or intentional?

and another question in scripting, how can I reserve original Host header?

Expected behavior

Screenshots (optional)

Originally created by @chaoyangnz on GitHub (Apr 8, 2021). Original GitHub issue: https://github.com/ProxymanApp/Proxyman/issues/852 Originally assigned to: @NghiaTranUIT on GitHub. ### Proxyman version? (Ex. Proxyman 1.4.3) latest ### macOS Version? (Ex. mac 10.14) 10.15 ### Steps to reproduce I am using scripting tool to add custom script, but i found the documentation saying `request.schema`. Normally we use `scheme` to refer to `http` or `https`. is that a typo or intentional? and another question in scripting, how can I reserve original `Host` header? ### Expected behavior ### Screenshots (optional)
kerem 2026-03-03 19:22:22 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@NghiaTranUIT commented on GitHub (Apr 8, 2021):

sorry, looks like it's a bug. I'm going to fix it now 👍

It should be the scheme

For the Preserve host, you can use request.preserveHostHeader

https://github.com/ProxymanApp/Proxyman/issues/769#issuecomment-770147548

Look like it doesn't mention in the Doc. I will update it

<!-- gh-comment-id:815401714 --> @NghiaTranUIT commented on GitHub (Apr 8, 2021): sorry, looks like it's a bug. I'm going to fix it now 👍 It should be the `scheme` For the Preserve host, you can use `request.preserveHostHeader ` https://github.com/ProxymanApp/Proxyman/issues/769#issuecomment-770147548 Look like it doesn't mention in the Doc. I will update it
Author
Owner

@chaoyangnz commented on GitHub (Apr 8, 2021):

thanks @NghiaTranUIT nice to document it. another question: how can I label Edited column in scripting just like built-in Map Remote does?

<!-- gh-comment-id:815404724 --> @chaoyangnz commented on GitHub (Apr 8, 2021): thanks @NghiaTranUIT nice to document it. another question: how can I label `Edited` column in scripting just like built-in `Map Remote` does?
Author
Owner

@NghiaTranUIT commented on GitHub (Apr 8, 2021):

By Default, it should be marked "Edited" as soon as the flow is modified by the script. If it's not, maybe it's a bug 😄

Let me double check it 👍

<!-- gh-comment-id:815405975 --> @NghiaTranUIT commented on GitHub (Apr 8, 2021): By Default, it should be marked "Edited" as soon as the flow is modified by the script. If it's not, maybe it's a bug 😄 Let me double check it 👍
Author
Owner

@chaoyangnz commented on GitHub (Apr 8, 2021):

@NghiaTranUIT it is not.

my script is like the below:

image

I only want to transparently forward my request to my localhost.

So in the flow list, the request will be put into localhost domain folder rather than the original develop.example.com domain folder with Edit column labeled.

I want to achieve the same behaviour like Map Remote

<!-- gh-comment-id:815406950 --> @chaoyangnz commented on GitHub (Apr 8, 2021): @NghiaTranUIT it is not. my script is like the below: ![image](https://user-images.githubusercontent.com/4645331/113961119-a6b0c700-9879-11eb-860b-7edc0dc854bc.png) I only want to transparently forward my request to my localhost. So in the flow list, the request will be put into `localhost` domain folder rather than the original `develop.example.com` domain folder with `Edit` column labeled. I want to achieve the same behaviour like `Map Remote`
Author
Owner

@NghiaTranUIT commented on GitHub (Apr 8, 2021):

Thanks. I will investigate why there is no Edited in the Scripting 👍

<!-- gh-comment-id:815448278 --> @NghiaTranUIT commented on GitHub (Apr 8, 2021): Thanks. I will investigate why there is no Edited in the Scripting 👍
Author
Owner

@NghiaTranUIT commented on GitHub (Apr 8, 2021):

@chaoyangnz please check out this beta build: https://github.com/ProxymanApp/Proxyman/issues/853#issuecomment-815522521

It fixes some bugs you mentioned 👍

So in the flow list, the request will be put into localhost domain folder rather than the original develop.example.com domain folder with Edit column labeled.

It's the correct behavior from the Scripting. I'm not sure how to fix it

<!-- gh-comment-id:815523473 --> @NghiaTranUIT commented on GitHub (Apr 8, 2021): @chaoyangnz please check out this beta build: https://github.com/ProxymanApp/Proxyman/issues/853#issuecomment-815522521 It fixes some bugs you mentioned 👍 > So in the flow list, the request will be put into localhost domain folder rather than the original develop.example.com domain folder with Edit column labeled. It's the correct behavior from the Scripting. I'm not sure how to fix it
Author
Owner

@chaoyangnz commented on GitHub (Apr 8, 2021):

@NghiaTranUIT thanks for the new version.

just wondering,

  • how can Map Remote achieved that?
  • can I use regex match with group capturing and then replace part of request path? e.g I expect to map a URL: http://example.com/aa/(.+) to http://aa.com/$1? I saw Charles can do this
<!-- gh-comment-id:815572635 --> @chaoyangnz commented on GitHub (Apr 8, 2021): @NghiaTranUIT thanks for the new version. just wondering, - how can `Map Remote` achieved that? - can I use regex match with group capturing and then replace part of request path? e.g I expect to map a URL: `http://example.com/aa/(.+)` to `http://aa.com/$1`? I saw Charles can do this
Author
Owner

@NghiaTranUIT commented on GitHub (Apr 8, 2021):

If you would like to achieve the same thing with Map Remote (like your screenshot in the previous comment https://github.com/ProxymanApp/Proxyman/issues/852#issuecomment-815406950), it's not possible for now 😿

If your script is working, please use it since it's easier to express your logic.

can I use regex match with group capturing and then replace part of request path? e.g I expect to map a URL: http://example.com/aa/(.+) to http://aa.com/$1? I saw Charles can do this

You can do it by using Regex in the Scripting.

Here is the example code

const regex = /http:\/\/example\.com\/aa\/(.+)/gm;
const str = `http://example.com/aa/v1/data?name=proxyman
`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

It will return the matching groups

Found match, group 0: http://example.com/aa/v1/data?name=proxyman
Found match, group 1: v1/data?name=proxyman

After that, you get the last group and construct the URL in the script 👍

<!-- gh-comment-id:815577069 --> @NghiaTranUIT commented on GitHub (Apr 8, 2021): If you would like to achieve the same thing with Map Remote (like your screenshot in the previous comment https://github.com/ProxymanApp/Proxyman/issues/852#issuecomment-815406950), it's not possible for now 😿 If your script is working, please use it since it's easier to express your logic. > can I use regex match with group capturing and then replace part of request path? e.g I expect to map a URL: http://example.com/aa/(.+) to http://aa.com/$1? I saw Charles can do this You can do it by using Regex in the Scripting. Here is the example code ```js const regex = /http:\/\/example\.com\/aa\/(.+)/gm; const str = `http://example.com/aa/v1/data?name=proxyman `; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); } ``` It will return the matching groups ``` Found match, group 0: http://example.com/aa/v1/data?name=proxyman Found match, group 1: v1/data?name=proxyman ``` After that, you get the last group and construct the URL in the script 👍
Author
Owner

@chaoyangnz commented on GitHub (Apr 8, 2021):

thanks. close this.

<!-- gh-comment-id:815578671 --> @chaoyangnz commented on GitHub (Apr 8, 2021): thanks. close this.
Author
Owner

@NghiaTranUIT commented on GitHub (Apr 8, 2021):

Thank you. I will create a ticket to improve the Map Remote, which is able to use Regex Group like Charles 👍

<!-- gh-comment-id:815580385 --> @NghiaTranUIT commented on GitHub (Apr 8, 2021): Thank you. I will create a ticket to improve the Map Remote, which is able to use Regex Group like Charles 👍
Author
Owner

@patrickschulze commented on GitHub (Jun 19, 2024):

@NghiaTranUIT I bought Proxyman yesterday and I really like it.

However, is this functionality already implemented when using the UI and not scripting? How would I access the matching groups? I tried named groups as well as numbered groups but it didn't seem to work.

Rule
https://test.com/b2c/app/pushnotification/1/(.+?)/partner/register-token

Neither
/api/gsa/pushnotification/$1/partner/register-token or /api/gsa/pushnotification/${1}/partner/register-token work.

<!-- gh-comment-id:2177958525 --> @patrickschulze commented on GitHub (Jun 19, 2024): @NghiaTranUIT I bought Proxyman yesterday and I really like it. However, is this functionality already implemented when using the UI and not scripting? How would I access the matching groups? I tried named groups as well as numbered groups but it didn't seem to work. **Rule** `https://test.com/b2c/app/pushnotification/1/(.+?)/partner/register-token` Neither `/api/gsa/pushnotification/$1/partner/register-token` or `/api/gsa/pushnotification/${1}/partner/register-token` work.
Author
Owner

@NghiaTranUIT commented on GitHub (Jun 19, 2024):

@patrickschulze sorry, the Matching Group (() and ${1}) hasn't been supported for the UI yet.

I highly recommend using Scripting Tool, because it's easier to express your needs by writing a JS code.

<!-- gh-comment-id:2177970561 --> @NghiaTranUIT commented on GitHub (Jun 19, 2024): @patrickschulze sorry, the Matching Group (`() and ${1}`) hasn't been supported for the UI yet. I highly recommend using Scripting Tool, because it's easier to express your needs by writing a JS code.
Author
Owner

@patrickschulze commented on GitHub (Jun 19, 2024):

@NghiaTranUIT Thanks for the very quick response. That's actually what I'm already looking into since scripting will be the better option in the future anyway when it comes to efficiency.

Would still be a nice to have in the UI I guess since Proxyman's UI is really nice.

<!-- gh-comment-id:2178010985 --> @patrickschulze commented on GitHub (Jun 19, 2024): @NghiaTranUIT Thanks for the very quick response. That's actually what I'm already looking into since scripting will be the better option in the future anyway when it comes to efficiency. Would still be a nice to have in the UI I guess since Proxyman's UI is really nice.
Author
Owner

@patrickschulze commented on GitHub (Jun 19, 2024):

Just if anyone is curious how to solve it with named groups, here's what I came up with:

const REGEX = /https:\/\/test\.com\/b2c\/app\/pushnotification\/(?<apiVersion>\d+?)\/(?<sapClientId>\d+?)\/(?<controllerName>\w+)\/(?<endpointName>[\w-]+)/

const ENDPOINT_TO_METHOD_MAPPING = {
  partner: {
    'register-token': 'POST',
    'delete-token': 'PATCH'
  },
  notification_settings: {
    'all': 'GET',
    'update': 'PATCH'
  }
}

async function onRequest(context, url, request) {
  const match = url.match(REGEX)
  
  if (match) {
    const { apiVersion, sapClientId, controllerName, endpointName } = match.groups

    if (request.method !== ENDPOINT_TO_METHOD_MAPPING?.[controllerName]?.[endpointName]) {
      console.log('Invalid METHOD')
      
      return {}
    }
    
    request.scheme = 'http'
    request.host = 'localhost'
    request.port = 3000
    request.path = `api/gsa/pushnotification/${sapClientId}/${controllerName}/${endpointName}`
  } else {
    console.log('does not match')
  }

  return request
}
<!-- gh-comment-id:2178112186 --> @patrickschulze commented on GitHub (Jun 19, 2024): Just if anyone is curious how to solve it with named groups, here's what I came up with: ``` const REGEX = /https:\/\/test\.com\/b2c\/app\/pushnotification\/(?<apiVersion>\d+?)\/(?<sapClientId>\d+?)\/(?<controllerName>\w+)\/(?<endpointName>[\w-]+)/ const ENDPOINT_TO_METHOD_MAPPING = { partner: { 'register-token': 'POST', 'delete-token': 'PATCH' }, notification_settings: { 'all': 'GET', 'update': 'PATCH' } } async function onRequest(context, url, request) { const match = url.match(REGEX) if (match) { const { apiVersion, sapClientId, controllerName, endpointName } = match.groups if (request.method !== ENDPOINT_TO_METHOD_MAPPING?.[controllerName]?.[endpointName]) { console.log('Invalid METHOD') return {} } request.scheme = 'http' request.host = 'localhost' request.port = 3000 request.path = `api/gsa/pushnotification/${sapClientId}/${controllerName}/${endpointName}` } else { console.log('does not match') } return request } ```
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#847
No description provided.