Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add filter predicate on session.setCookiesFromResponse #616

Open
pocesar opened this issue Feb 29, 2020 · 0 comments
Open

add filter predicate on session.setCookiesFromResponse #616

pocesar opened this issue Feb 29, 2020 · 0 comments
Labels
t-tooling Issues with this label are in the ownership of the tooling team.

Comments

@pocesar
Copy link
Contributor

pocesar commented Feb 29, 2020

sometimes you want just one or a couple of cookies from the response. this could be done using an optional filter predicate function or array as a second parameter:

session.setCookiesFromResponse(response, (name, value, allCookiesArray) => {
  // filter function, returns true for including, false otherwise
  return ['PHPSESSID'].includes(name) 
      && allCookiesArray.some((s) => s.name === '_sess');
});
session.setCookiesFromResponse(response, ['PHPSESSID']); // pick cookie name from array

like the Array.filter function, the callback 3rd parameter contains an array of all parsed cookies. this is useful if you want to only include a cookie if another one is present.

at the moment having to resort to:

    if (response.headers['set-cookie'] && response.headers['set-cookie'].length) {
      response.headers['set-cookie'] = response.headers['set-cookie'].filter((s) => {
        return [/* cookie names */].some((key) => {
          return s.startsWith(`${key}=`);
        });
      });
    }

    session.setCookiesFromResponse(response);

session.setPuppeteerCookies accepts a plain { name, value }[], so it's easier to filter before passing to the function

@mtrunkat mtrunkat added the t-tooling Issues with this label are in the ownership of the tooling team. label Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
t-tooling Issues with this label are in the ownership of the tooling team.
2 participants