# How can I get the cookies from an HTTP response?

**URL:** <https://discourse.julialang.org/t/how-can-i-get-the-cookies-from-an-http-response/85454>\
**Category:** New to Julia\
**Tags:** question, http\
**Created:** [August 8, 2022, 1:45am UTC](https://discourse.julialang.org/t/how-can-i-get-the-cookies-from-an-http-response/85454 "2022-08-08T01:45:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkjohnson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkjohnson/32/211111_2.png) [@mkjohnson](https://discourse.julialang.org/u/mkjohnson)\
**Post date:** [August 8, 2022, 1:45am UTC](https://discourse.julialang.org/t/how-can-i-get-the-cookies-from-an-http-response/85454/1 "2022-08-08T01:45:32Z")

</div>

I’m working with an API where I’m trying to grab one of the cookies from the response of a `POST` request.

Coming from R - with the `httr` package, I can do something like this because `cookies` is a field in the response, much like status code, headers, etc:

```nohighlight
response <- POST(url,
                 add_headers(.headers = c("Content-Type" = "application/json;charset=UTF-8")),
                 body = request_body)

token <-
    response |>
    pluck("cookies") |>
    filter(name == "cookie_name") |>
    pull(cookie_value)

```

In `HTTP.jl`, the only things I see in the documentation are version, status, headers, body, and request. Is there something like `HTTP.response.cookies` available that I’m missing?

My uninformed, hacky solution is to get the cookie from the header like this, but my emphasis is on _hacky_:

```julia
function get_cookie(resp::HTTP.Messages.Response; cookie_name::String)
    cookie_headers = filter(headers -> headers[1] == "Set-Cookie", resp.headers)

    # get the index where the cookie is
    idx = map(1:length(cookie_headers)) do x 
        cookie_headers[x][2] |>
        cookie_values -> occursin(cookie_name, cookie_values)
    end 

    # get the cookie values
    cookie_values = map(1:length(cookie_headers)) do x 
        cookie_headers[x][2]
    end 
    
    # index into the values to extract the cookie in question
    # (I already know the length of the token in question...)
    cookie_values[idx][1][10:559]
end

get_cookie(response, cookie_name = "name_of_the_cookie")
#> "a string containing the token in question"

```

---

<div class="post-metadata">

**Author:** ![BenPH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/benph/32/38619_2.png) [@BenPH](https://discourse.julialang.org/u/BenPH)\
**Post date:** [August 8, 2022, 2:27am UTC](https://discourse.julialang.org/t/how-can-i-get-the-cookies-from-an-http-response/85454/2 "2022-08-08T02:27:36Z")

</div>

Looks like you can use [HTTP.Cookies.cookies](https://juliaweb.github.io/HTTP.jl/stable/reference/#HTTP.Cookies.cookies) on the response.

---

<div class="post-metadata">

**Author:** ![mkjohnson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkjohnson/32/211111_2.png) [@mkjohnson](https://discourse.julialang.org/u/mkjohnson)\
**Post date:** [August 8, 2022, 2:42am UTC](https://discourse.julialang.org/t/how-can-i-get-the-cookies-from-an-http-response/85454/3 "2022-08-08T02:42:01Z")

</div>

That gives me a method error:

```julia
resp = HTTP.request("POST", request_url, request_headers, request_body)
HTTP.Cookies.cookies(resp)

#> ERROR: MethodError: no method matching cookies(::HTTP.Messages.Response)
#> Closest candidates are:
#> cookies(::HTTP.Messages.Request) at ~/.julia/packages/HTTP/aTjcj/src/cookies.jl:314

```

Which seems odd, as the documentation seems to suggest that there _is_ a method for the response type.

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [August 8, 2022, 5:27am UTC](https://discourse.julialang.org/t/how-can-i-get-the-cookies-from-an-http-response/85454/4 "2022-08-08T05:27:43Z")

</div>

You probably just need to update; `HTTP.cookies(resp)` was added in the 1.0 release, but wasn’t available in the 0.9.17 release.

---

<div class="post-metadata">

**Author:** ![mkjohnson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkjohnson/32/211111_2.png) [@mkjohnson](https://discourse.julialang.org/u/mkjohnson)\
**Post date:** [August 8, 2022, 12:41pm UTC](https://discourse.julialang.org/t/how-can-i-get-the-cookies-from-an-http-response/85454/5 "2022-08-08T12:41:20Z")

</div>

Aha - yep, being behind on updates will do that 😅

What I end up doing then is:

```julia
cookies = HTTP.cookies(resp)

token_value = filter(x -> x.name == "cookie_name", cookies)[1].value

```
