# Extract individual cookie from multiple cookies

**URL:** <https://www.ruby-forum.com/t/extract-individual-cookie-from-multiple-cookies/204080>\
**Category:** NGINX\
**Created:** [March 4, 2011, 3:57pm UTC](https://www.ruby-forum.com/t/extract-individual-cookie-from-multiple-cookies/204080 "2011-03-04T15:57:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [March 4, 2011, 3:57pm UTC](https://www.ruby-forum.com/t/extract-individual-cookie-from-multiple-cookies/204080/1 "2011-03-04T15:57:12Z")

</div>

Hi,

Am developing an nginx module. I get multiple cookies in a http  
request. I need to extract individual cookie from the multiple cookies  
that I get. How do I achive this?

Is there an api to extract individual cookie?

> From what I understand:

- The cookies are present in request-\>headers\_in as array

- The length of this array is 1, even when we get multiple cookies.

- Further investigation reveals that the element is a hash.

- So will I need to parse the cookie string in “value” to extract  
individual cookies?

- I cannot use ngx\_http\_parse\_multi\_header\_lines() as I may not always  
know the name of the cookie (the cookie name may vary depending on  
id).  
Thanks for your help.

~ s

---

<div class="post-metadata">

**Author:** ![shirish\_deshpande](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@shirish\_deshpande](https://www.ruby-forum.com/u/shirish_deshpande)\
**Post date:** [March 5, 2011, 10:56am UTC](https://www.ruby-forum.com/t/extract-individual-cookie-from-multiple-cookies/204080/2 "2011-03-05T10:56:11Z")

</div>

Hello!

On Fri, Mar 04, 2011 at 08:25:46PM +0530, shirish deshpande wrote:

> Hi,
> 
> Am developing an nginx module. I get multiple cookies in a http  
> request. I need to extract individual cookie from the multiple cookies  
> that I get. How do I achive this?
> 
> Is there an api to extract individual cookie?

ngx\_http\_parse\_multi\_header\_lines()

See ngx\_http\_userid\_module.c for examples.

> > From what I understand:
> 
> - The cookies are present in request-\>headers\_in as array

Yes (incorrect syntax, but doesn’t really matter).

> - The length of this array is 1, even when we get multiple cookies.

No. This won’t be 1 as long as you get multiple headers from  
client.

> - Further investigation reveals that the element is a hash.

This is header and it’s value.

> - So will I need to parse the cookie string in “value” to extract  
> individual cookies?

The ngx\_http\_parse\_multi\_header\_lines() is a api to extract  
individual cookies.

> - I cannot use ngx\_http\_parse\_multi\_header\_lines() as I may not always  
> know the name of the cookie (the cookie name may vary depending on  
> id).

If you don’t know id, you don’t need an api to extract individual  
cookies. Guess you want to iterate all cookies, right?

There is no such api, though iterating over r-\>headers\_in.cookies  
and parsing values should do the trick. Refer to  
ngx\_http\_parse\_multi\_header\_lines() implementation for an example.

Maxim D.
