0

I'm trying to use UrlfetchApp to send a request to a page. Request is good. But returning

<p>Your browser is currently set to block cookies. Please enable cookies in your browser preferences and try again.</p>

in the html body.

Here is the code:

const res1 = UrlFetchApp.fetch('https://url.com', {
method: 'POST',
headers: {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Accept-Language': 'en-US,en;q=0.9',
    'Cache-Control': 'max-age=0',
    'Connection': 'keep-alive',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Cookie': 'JSESSIONID=Dfaefaefaesfsaefgr',
    'Origin': 'https://url.com',
    'Referer': 'https://url.com',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-User': '?1',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36',
    'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"'
},
payload: 'payloaddata'});

I don't know how to turn on the cookie when using UrlFetch function. Does anyone has the same issue? Thanks for any suggestions.

What I have tried:

  1. Seperate Cookie property from headers into a json object.

    cookies = {
    'JSESSIONID': '778C494754356A23F080849C10F2A851',
    'TS01ee6e39': '018c1954d58470c0adb8d6d0df850dba9363aa54c16fc4653b2178278f62a5f0dde96d5780b3338857c8ea3ff92c6e99b9bd3a5867cef327bd847ac05ab9242a7414fa0832',
    'X-HR-ClientSessionId': '10_107.162.4.39_1651626597548',
    'locale': 'en',
    'TS0189a565': '018c1954d5ae31eb4b4d18a85d43414cdcd9158bcc6fc4653b2178278f62a5f0dde96d578016166d716abd84dc80b66117923674fd68b4dd222e33d15361b818320e9dbd03333b4682ebc552fb370cd462eb3e5d2b5cc5273e789e87f2bd772fa800fa9e77744d459169cf3d8594422a3d7ae7968ba33103e373fbcf2c83f38da92d9643e5f9ef6a925938338bef881e38e827bf0a5635126e7297731cc06d71fd1a883702',}
    

But this does not work, request immediately return session expired. Please try again.

Edit on 05/07/2022:

I'm trying to get the JSESSIONID from fetch request, I can find this object on the browser, But could not find it on the urlfetch return response.

liquidkat
  • 566
  • 4
  • 12
  • Could you share a [minimal example](https://stackoverflow.com/help/minimal-reproducible-example) that reproduces the issue? – Gabriel Carballo May 03 '22 at 16:29
  • In order to correctly understand your current situation, can you provide the sample URL for replicating your issue? By the way, when I saw your showing script, `body: 'payloaddata'` is used as the 2nd argument of `fetch` method. I think that the property of `body` is not existing. Please be careful about this. [Ref](https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetchurl,-params) – Tanaike May 04 '22 at 01:45
  • @Tanaike I used https://curlconverter.com/ to covert the session to these codes. It works in the Python Environment. But when I try to convert into fetch, it does not work and return the error in HTML format. I do not know how to enable cookies when using fetch methods. – liquidkat May 04 '22 at 03:04
  • Thank you for replying. From `I used curlconverter.com to covert the session to these codes. It works in the Python Environment. But when I try to convert into fetch, it does not work and return the error in HTML format. I do not know how to enable cookies when using fetch methods.`, you cannot provide the sample URL. Is my understanding correct? And, can you provide your python script? – Tanaike May 04 '22 at 03:10
  • @Tanaike the url is https://ows01.hireright.com/login/. With the right cookie I'm able to get what I need from the FetchApp return. But the problem is I'm not able to get the right cookies from request. I tried to get Set-Cookies from response.getAllheaders(). But seems the cookies are incomplete and not able to find Jsessionid object. – liquidkat May 07 '22 at 16:36
  • 1
    Thank you for replying. When I tested your provided URL, I cannot confirm `Your browser is currently set to block cookies.`. So, can I ask you about the detailed flow for correctly replicating to retrieve `Your browser is currently set to block cookies.`? First, in order to correctly understand your situation, I would like to replicate your situation. – Tanaike May 07 '22 at 23:48
  • Possible related : [Cookie handling in Google Apps Script - How to send cookies in header?](https://stackoverflow.com/q/10869932/1595451) – Rubén May 08 '22 at 00:13

0 Answers0