cli icon indicating copy to clipboard operation
cli copied to clipboard

Cookies with "Domain=localhost" aren't getting stored in session file

Open pluttrell opened this issue 8 years ago • 10 comments

If I use httpie to make a call that returns cookies to a localhost address, such as:

http --session=./session.json POST http://localhost:9081/cookieTest

which sends back a header like this:

Set-Cookie: c1=test; Max-Age=298; Expires=Tue, 15-Aug-2017 19:17:58 GMT; Domain=localhost; Path=/; HttpOnly

but the session.json only includes the following:

{
    "__meta__": {
        "about": "HTTPie session file",
        "help": "https://httpie.org/docs#sessions",
        "httpie": "0.9.9"
    },
    "auth": {
        "password": null,
        "type": null,
        "username": null
    },
    "cookies": {},
    "headers": {
        "Accept": "application/json, */*"
    }
}

If I try this exact same process with the same code hosted at somedomain.com, it works perfectly. For example:

http --session=./session.json POST http://somedomain.com/cookieTest

which sends back a header like this:

Set-Cookie: c1=test; Max-Age=298; Expires=Tue, 15-Aug-2017 19:17:58 GMT; Domain=somedomain.com; Path=/; HttpOnly

and the session.json includes the following:

{
    "__meta__": {
        "about": "HTTPie session file",
        "help": "https://httpie.org/docs#sessions",
        "httpie": "0.9.9"
    },
    "auth": {
        "password": null,
        "type": null,
        "username": null
    },
    "cookies": {
        "c1": {
            "expires": 1502824948,
            "path": "/",
            "secure": false,
            "value": "test"
        }
    },
    "headers": {
        "Accept": "application/json, */*"
    }
}

Is there any way to get it to work with localhost? Or is this a bug?

pluttrell avatar Aug 15 '17 19:08 pluttrell

It seems to be a behavior inherited from requests. Alsocookie is being recognized and parsed if you don't setDomain, but not if domain is explicitly set to localhost.

makarchuk avatar Oct 05 '17 18:10 makarchuk

Related #143

jkbrzt avatar Jun 18 '20 23:06 jkbrzt

I just ran into the same problem (httpie not saving cookie in session when domain is set to localhost).

Is there a workaround within httpie?

The only way I can fix it is by not sending a domain in the cookie when serving from localhost:

$domain = (getenv('HTTP_HOST') == 'localhost' ? null : getenv('HTTP_HOST'));

quinncomendant avatar Jun 11 '22 04:06 quinncomendant