Extract session id from apex url using regular expression

extract session id from apex url

In this article, we will explore the process of extracting a session ID from an APEX URL using regular expressions. We will also demonstrate how to achieve this task in both Cypress and JavaScript.

When you visit an APEX application URL, it often includes a session ID within the URL itself. This session ID is essential for subsequent visits to different URLs within the application. Let’s take a look at an example APEX URL:

Example APEX URL: http://apex.somewhere.com/pls/apex/f?p=4350:1:220883407765693447

In the URL above, the long number at the end represents the session ID generated by the APEX application server.

To extract the session ID, you can use the following regular expression, which returns two matching groups, with the second group containing the session ID:

Regular Expression: /f?p=4350:1:(\d+):/

In the regular expression, “4350” represents the application ID, and “1” represents the page ID. Make sure to replace these values with your application’s ID and the relevant page ID.

Here’s a JavaScript function that extracts the session ID from an APEX URL using the specified regular expression:

javascriptCopy code

export function getSessionIdFromUrl(link) { let sessionId = link.match(/f\?p=4350:1:(\d+):/)[1]; return sessionId; }

You can also achieve the same result in Cypress. The cy.url() method retrieves the current URL, which can then be passed to a callback function to extract the session ID. Here’s an example:

javascriptCopy code

let sessionId; const url = ‘http://apex.somewhere.com/pls/apex/f?p=4350:1:220883407765693447’; cy.url().then((url) => { sessionId = url.match(/f\?p=4350:1:(\d+)/)[1]; return sessionId; });

Now you have a clear understanding of how to extract the session ID from an APEX URL using regular expressions in both Cypress and JavaScript. This knowledge can be invaluable when working with APEX applications.

http://apex.somewhere.com/pls/apex/f?p=4350:1:220883407765693447

the long number at the end is the session id returned by the apex application server.

The below regular expression will return two matching groups and the second group contains session id.

/f?p=4350:1:(\d+):/

In the above regular expression 4350 is app id and 1 is page id replace these with your application id and relevant page id.

JavaScript function to extract session Id from apex url using regular expression.

export function getSessionIdFromUrl(link) {
  let sessionId = link.match(/f\?p=4350:1:(\d+):/)[1]
  return sessionId;
}

Now, same thing can be achieved in cypress. cy.url() method will yields the current url and this in turn can be passed to then call back function to extract session id.

let sessionId;
url = http://apex.somewhere.com/pls/apex/f?p=4350:1:220883407765693447
cy.url().then((url) => {
      sessionId= url.match(/f\?p=4350:1:(\d+)/)[1]
      return sessionId;
});

source: stack overflow