# How do I manually set a cookie in a script?

> Loadster handles most cookies automatically, but occasionally you might need to set one yourself. Here's how to set a cookie in your Loadster script.

Source: https://loadster.com/faqs/manually-set-a-cookie/

Simply put, cookies are a way to convey state in the otherwise stateless HTTP protocol.
Many sites use them to store and relay information about the user from one request to the next.

Once a cookie is set, the client or browser is expected to send that cookie with every
subsequent request to that same server (sometimes only to certain paths).

Cookies are normally set by the _Set-Cookie_ header, but they can also be set by JavaScript. You can read more
about the Cookie header on the
[Mozilla Developer Network](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie).

## Setting cookies in protocol scripts

In most cases, it's not necessary to manually set a _Cookie_ header in a protocol script. Loadster
automatically receives _Set-Cookie_ headers from your server and handles the cookies like any
other HTTP user agent.

However, if you need to manually pass a cookie from your script that was not received from the
server, here is how you would do it.

First of all, click the **Add...** menu on the step you want to add the cookie for, and choose **Add request header**.

_Figure: Adding a custom header_


Enter the header name of "Cookie" and the header value as the key-value pair.
For this example, we'll go with a cookie name of "a" and a value of "1".

_Figure: Setting a Cookie header_


After playing your script, you'll see a message about the custom request header in the logs:

_Figure: The custom Cookie header appears in the logs_


You can also drill into the request to view the exact headers that were sent:

_Figure: The custom Cookie header appears in the results tree_


## Setting cookies in browser scripts

Browser Bots automate real web browsers. Since the browser handles cookies automatically, and in most cases you
don't need to worry about setting them yourself.

However, there are times you might want to set a cookie manually. For example, you might be testing a site that requires
login, but you're not actually performing the login in the script. Instead you could manually set the cookies that
you'd otherwise get by logging in.

### Setting cookies in Evaluate Blocks

One way to accomplish this in a browser script is with an [evaluate block](https://loadster.com/manual/browser-scripts/evaluate-blocks/).
Evaluate blocks let you execute custom JavaScript inside the bot's browser.

An evaluate block that sets a cookie would go something like this:

```javascript
// Declare a function that sets a cookie
function setCookie(cookieName, cookieValue, expiresInDays) {
  const date = new Date();
  const expiresInMillis = expiresInDays * 86400000;

  date.setTime(date.getTime() + expiresInMillis);

  document.cookie = cookieName + '=' + cookieValue + ';expires=' + date.toUTCString() + ';path=/';
}

// Call the function to actually set a cookie
setCookie('myCookieName', 'myCookieValue', 10)
```


Of course, you can call the function multiple times if you need to set multiple cookies.

Since the JavaScript runs on-page, setting a cookie in an eval block requires that you've already navigated to a page,
so it won't work as the first step in your script.

### Setting cookies in Code Blocks

You can also set a cookie from a [code block](https://loadster.com/manual/code-blocks/).

```javascript
browser.addCookie({
  name: 'myCookieName',
  value: 'myCookieValue',
  url: 'https://example.com/secure'
});
```


The cookie will be available to the URL you specify and all sub-URLs.

Since code blocks run outside the bot's browser, you can use them to set a cookie before navigating to a page.

