/    Sign up×
Community /Pin to ProfileBookmark

How to pass a cookie to a websocket handshake and an ajax request?

I did some research but could not solve this issue. I have an app which does two things. For both of them I want to pass the same cookie:

**a) Ajax request with axios:**

“`axios.get(`https://api.example.com`, {withCredentials: true}) “`

**b) Websocket handshake:**

“` let socket = new WebSocket(`wss://delivery.example.com`) “`

The cookie looks like this:

“` access_token=eyJhbGci; path=/; expires=Thu, 10 Oct 2019 12:24:24 GMT; domain=example.com; samesite=strict; secure; httponly “`

Option “`a)“` works if I set the domain flag to “`api.example.com“`.

Option “`b)“` works if I set the domain flag to “`example.com“`.

Bizarrely “`example.com“` does not work for option “`a)“`.

How can I make both of the option work with the same cookie?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 08.2019 — Maybe domain=*.example.com ? (Just speculation, no promises)
Copy linkTweet thisAlerts:
@thiloilgauthorOct 08.2019 — @NogDog#1609872 let me try xD
Copy linkTweet thisAlerts:
@NogDogOct 08.2019 — Oh, it might just be a leading dot (no astersisk). At least with the PHP setcookie() function, for domain it says:

> The (sub)domain that the cookie is available to. Setting this to a subdomain (such as 'www.example.com') will make the cookie available to that subdomain and all other sub-domains of it (i.e. w2.www.example.com). To make the cookie available to the whole domain (including all subdomains of it), simply set the value to the domain name ('example.com', in this case).

> **Older browsers still implementing the deprecated » RFC 2109 may require a leading . to match all subdomains.** [my emphasis]
×

Success!

Help @thiloilg spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.10,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...