# Get document.cookie field value in html response body

**URL:** <https://community.postman.com/t/get-document-cookie-field-value-in-html-response-body/52634>\
**Category:** Help Hub\
**Tags:** cookies\
**Created:** [September 30, 2023, 3:00pm UTC](https://community.postman.com/t/get-document-cookie-field-value-in-html-response-body/52634 "2023-09-30T15:00:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![u9119](https://avatars.discourse-cdn.com/v4/letter/u/8baadc/32.png) [@u9119](https://community.postman.com/u/u9119)\
**Post date:** [September 30, 2023, 3:00pm UTC](https://community.postman.com/t/get-document-cookie-field-value-in-html-response-body/52634/1 "2023-09-30T15:00:04Z")

</div>

![Untitled](https://us1.discourse-cdn.com/getpostman/original/3X/0/1/0111e53550864784b62f2663c0d54cf93b3bcac0.png)

API response is a html body. it contains document.cookies in script tag. I want to get document.cookie.value of the field “gt=”. how can i get this field value using test scripts ?

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [September 30, 2023, 3:37pm UTC](https://community.postman.com/t/get-document-cookie-field-value-in-html-response-body/52634/2 "2023-09-30T15:37:30Z")

</div>

Hi @u9119

Sounds like a RegEx would help… have a look at this post…

> [@How to to get elements value from html response at \<script\> \</script\>](https://community.postman.com/t/how-to-to-get-elements-value-from-html-response-at-script-script/35913):
>
> So I get a response something like this: \<!DOCTYPE html\> \<html\> \<head\> -------------------- \</head\> \<body\> \<script\> function onClickButton(word) { if (word == 'yes'){ var authorizationCode = '360ad5ce-ecfe-4ad4-83d1-9254e89a3ccc'; var state = 'c8271b81-4229-6a1f-bf9c-758f11c1f5b1'; } else { alert(word); } } \</script\> \<div class="shb-psua-sign-app-sandbox-container"\> -------------------- \</div\> \</body\> \</html\>…

---

<div class="post-metadata">

**Author:** ![u9119](https://avatars.discourse-cdn.com/v4/letter/u/8baadc/32.png) [@u9119](https://community.postman.com/u/u9119)\
**Post date:** [October 1, 2023, 4:21am UTC](https://community.postman.com/t/get-document-cookie-field-value-in-html-response-body/52634/3 "2023-10-01T04:21:23Z")

</div>

This works.

```auto
function getCookie(name) { 
    var re = new RegExp(name + "=([^;]+)"); 
    var value = re.exec(pm.response.text()); 
    return (value != null) ? decodeURI(value[1]) : null; 
}

console.log(parseInt(getCookie('gt')));

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [October 4, 2023, 4:21am UTC](https://community.postman.com/t/get-document-cookie-field-value-in-html-response-body/52634/4 "2023-10-04T04:21:37Z")

</div>

Closed on October 4, 2023, 4:21am UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
