> For the complete documentation index, see [llms.txt](https://ploegert.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ploegert.gitbook.io/til/programmy/rails/verify-and-read-a-signed-cookie-value.md).

# Verify And Read A Signed Cookie Value

Let's say a value was added as a [signed cookie](https://apidock.com/rails/ActionDispatch/Cookies/CookieJar/signed) in a request:

```ruby
cookies.signed[:discount] = 45
#=> Set-Cookie: discount=BAhpMg==--2c1c6906c90a3bc4fd54a51ffb41dffa4bf6b5f7; path=/
```

Generally to verify and read that value, you'd grab it from the signed cookies included in the request.

```ruby
cookies.signed[:discount]
#=> 45
```

What if you have the signed cookie value, but not in the context of a `cookies` object?

You can build a cookie jar from the current request and read the verified value from that.

```ruby
cookie_value = 'BAhpMg==--2c1c6906c90a3bc4fd54a51ffb41dffa4bf6b5f7'
cookie_hash = { discount: cookie_value }

cookie_jar = ActionDispatch::Cookies::CookieJar.build(request, cookie_hash)

cookie_jar.signed[:discount]
#=> 45
```

It is also possible to [Base64 decode the value](https://blog.bigbinary.com/2013/03/19/cookies-on-rails.html), however that doesn't ensure that the value hasn't been tampered with.

[source](https://philna.sh/blog/2020/01/15/test-signed-cookies-in-rails/)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/rails/verify-and-read-a-signed-cookie-value.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
