Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot set > 1 cookies #106

Closed
murdercode opened this issue Dec 17, 2015 · 8 comments
Closed

Cannot set > 1 cookies #106

murdercode opened this issue Dec 17, 2015 · 8 comments

Comments

@murdercode
Copy link

When I try to set 2 or more cookies I give only the first one

case HTTPoison.get("https://httpbin.org/cookies", %{}, hackney: [cookie: [{"cookie1", "1"} , {"cookie2", "2"}]]) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
IO.puts body
{_, %HTTPoison.Response{status_code: _, body: _}} ->
# Nothing to do
end
end

(Sorry for eventually mistakes, i cleanup a complex code for write here) :)

@murdercode
Copy link
Author

Solved

case HTTPoison.get("https://httpbin.org/cookies", %{}, hackney: [cookie: "cookie1=1 cookie=2"]) do

@edgurgel
Copy link
Owner

I wonder if we should support the first instead of the one that is working..

@aphillipo
Copy link

Yes, I was looking at the test_case and it passes an array to cookie with a tuple in it... This isn't working for me!

I will try the second way... And it still doesn't work.

Maybe I need to set domain and path on these cookies?

@edgurgel edgurgel reopened this Dec 21, 2015
@murdercode
Copy link
Author

I tried to set domain and path but without success. The only way for me was to integrate a recall in phantomjs/casperjs, that's not a clean solution but it works.

With the second way I got my script working: https://i.imgur.com/bYu0Xvb.png

@nscyclone
Copy link
Contributor

Not sure whether someone is still interested in this but it looks like I finally could set several cookies and specify their domain, path and other options. Here is a working example:

iex(1)> HTTPoison.get("https://httpbin.org/headers", %{}, hackney: [cookie: [{"foo", "0", [{:path, "/headers"}, {:domain, ".httpbin.org"}, {:secure, true}, {:max_age, 300}]}, {"bar", "1", [{:path, "/"}, {:domain, ".httpbin.org"}]}]])
{:ok,
 %HTTPoison.Response{body: "{\n  \"headers\": {\n    \"Cookie\": \"foo=0; Version=1; Expires=Sat, 14-May-2016 14:05:33 GMT; Max-Age=300; Domain=.httpbin.org; Path=/headers; Secure, bar=1; Version=1; Domain=.httpbin.org; Path=/\", \n    \"Host\": \"httpbin.org\", \n    \"User-Agent\": \"hackney/1.6.0\"\n  }\n}\n",
  headers: [{"Server", "nginx"}, {"Date", "Sat, 14 May 2016 14:00:32 GMT"},
   {"Content-Type", "application/json"}, {"Content-Length", "263"},
   {"Connection", "keep-alive"}, {"Access-Control-Allow-Origin", "*"},
   {"Access-Control-Allow-Credentials", "true"}], status_code: 200}}

In my opinion, it would be fine to update README or the cookies test to make this more clear. What do you think about this, @edgurgel?

@edgurgel
Copy link
Owner

That would be amazing! Could you send a PR updating both? 🎉

@nscyclone
Copy link
Contributor

I've just started playing around with different tests and now it looks like I hurried with the conclusions. It turned out that the above example is not working in HTTParrot and this is the expected behaviour. Actually I was mislead a little by one of the previous comments and the hackney docs: several cookie attributes are described there but according to the latest RFC the client must not send anything but a list of key=value separated by a semicolon and a space in the Cookie header. These attributes should be used by server inside the Set-Cookie headers instead and this was mentioned in an appropriate issue.
So, the correct working way to pass multiple cookies is the following (both HTTParrot and httpbin give the identical expected result):

iex(1)> HTTPoison.get!("http://httparrot.herokuapp.com/cookies", %{}, hackney: [cookie: ["foo=1; bar=2"]])
%HTTPoison.Response{
 body: "{\n  \"cookies\": {\n    \"foo\": \"1\",\n    \"bar\": \"2\"\n  }\n}",
 headers: [{"Connection", "keep-alive"}, ...], 
 status_code: 200
}

iex(2)> HTTPoison.get!("https://httpbin.org/cookies", %{}, hackney: [cookie: ["foo=1; bar=2"]])
%HTTPoison.Response{
 body: "{\n  \"cookies\": {\n    \"bar\": \"2\", \n    \"foo\": \"1\"\n  }\n}\n",
 headers: [{"Server", "nginx"}, ...], 
 status_code: 200
}

Finally, I updated the README and the cookies test to make this easy to find and use: #135.

@edgurgel
Copy link
Owner

Thanks for the detailed info 👍

@edgurgel edgurgel closed this as completed Sep 1, 2016
m1foley added a commit to polleverywhere/chaperon that referenced this issue Oct 25, 2018
Cookie headers in HTTP requests are only supposed to have a name and
value. Chaperon was previously just echoing back the response cookies
that came from the server, which included attributes like `Expires` and
`HttpOnly`. When web servers parsed the `HTTP_COOKIE` header it would
incorrectly create weird cookies like `HttpOnly` with a null value.

https://tools.ietf.org/html/rfc6265

Setting multiple cookies was also broken: the hackney `cookie` option
expects 1 cookie string joined with semicolons. It was previously
joining them with newlines which would not get parsed correctly by the
web server.

edgurgel/httpoison#106
m1foley added a commit to polleverywhere/chaperon that referenced this issue Oct 25, 2018
Cookie headers in HTTP requests are only supposed to have a name and
value. Chaperon was previously just echoing back the response cookies
that came from the server, which included attributes like `Expires` and
`HttpOnly`. When web servers parsed the `HTTP_COOKIE` header it would
incorrectly create weird cookies like `HttpOnly` with a null value.

https://tools.ietf.org/html/rfc6265

Setting multiple cookies was also broken: the hackney `cookie` option
expects 1 cookie string joined with semicolons. It was previously
joining them with newlines which would not get parsed correctly by the
web server.

edgurgel/httpoison#106
m1foley added a commit to polleverywhere/chaperon that referenced this issue Oct 25, 2018
Cookie headers in HTTP requests are only supposed to have a name and
value. Chaperon was previously just echoing back the response cookies
that came from the server, which included attributes like `Expires` and
`HttpOnly`. When web servers parsed the `HTTP_COOKIE` header it would
incorrectly create weird cookies like `HttpOnly` with a null value.

https://tools.ietf.org/html/rfc6265

Setting multiple cookies was also broken: the hackney `cookie` option
expects 1 cookie string joined with semicolons. It was previously
joining them with newlines which would not get parsed correctly by the
web server.

edgurgel/httpoison#106
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants