pigfox
pigfox

Reputation: 1401

Reading cookie from Wasm

I am attempting to read a cookie from Wasm but have not found any examples so I guessed, but I guessed wrong.

c := js.Global().Get("Cookie").Get("cookiename")
fmt.Println(c)

This gives me an error:

panic: syscall/js: call of Value.Get on undefined

Given that I have not found any documentation regarding reading cookies from Wasm. Is this even possible?

Upvotes: 0

Views: 1334

Answers (1)

Marc
Marc

Reputation: 21035

There are three issues here:

  • the field is on document, not body
  • the field name is cookie, not Cookie
  • the cookie object is a string, you need to parse it to find the cookie by name.

To get the cookie string, use the following:

cookies := js.Global().Get("document").Get("cookie").String()

You will then need to process the string to iterate through the cookies and extract the one with the desired name. See Get cookie by name

As always with wasm, figure out the javascript code first then convert it to wasm.

Upvotes: 2

Related Questions