0

I'm working with puppeteer and trying to send an xhr requesr after login. Using devtools network tab, I copied the request of interest using the fetch api option. I wrapped it in the page.evaluate function as below:

let x = await page.evaluate((SecureAuthToken) => {
  return fetch("/api/Search/PropertySearch", {
    headers: {
      "accept": "application/json, text/plain, */*",
      "accept-language": "en-US,en;q=0.9",
      "content-type": "application/json;charset=UTF-8",
      "sec-ch-ua":
        '" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"',
      "sec-ch-ua-mobile": "?0",
      "sec-ch-ua-platform": '"Chrome OS"',
      "sec-fetch-dest": "empty",
      "sec-fetch-mode": "cors",
      "sec-fetch-site": "same-origin",
      "secureauthtoken": SecureAuthToken,
    },
    referrerPolicy: "no-referrer",
    body: '{"Filters":[{"FieldId":9,"BundleChildren":.....}',
    method: "POST",
    mode: "cors",
    credentials: "include",
  });
},SecureAuthToken);

I'm passing in the SecureAuthToken. when I do

console.log(x)

The result is

[]

What am I doing wrong? Also is there a good way to debug this?

user1592380
  • 34,265
  • 92
  • 284
  • 515
  • You're returning a promise object, not something you can pass between processes. Wait for the `fetch` to resolve inside `evaluate` (the browser console), then return the raw JSON back to Node from the browser console. – ggorlen Apr 16 '22 at 15:14
  • Thank you, In my case I changed the end of the code to "}).then(data => {return data.json()}); },SecureAuthToken); - This worked for me . So it appears no cookies are necessary and the fetch rather than node fetch option will work at least in my case. – user1592380 Apr 16 '22 at 15:40
  • You might even just make the fetch from Node rather than the browser, if possible... – ggorlen Apr 16 '22 at 15:41
  • Does this answer your question? [How do i return a value from page.evaluate() in puppeteer?](https://stackoverflow.com/questions/57570538/how-do-i-return-a-value-from-page-evaluate-in-puppeteer) – ggorlen Apr 16 '22 at 15:43

0 Answers0