question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bug: document.cookie does not seem to work at all with capacitor/core http

See original GitHub issue

Bug Report

I may be going crazy, so if this isn’t functionality that is supposed to work, please let me know.

Capacitor Version

💊   Capacitor Doctor  💊

Latest Dependencies:

  @capacitor/cli: 4.4.0
  @capacitor/core: 4.4.0
  @capacitor/android: 4.4.0
  @capacitor/ios: 4.4.0

Installed Dependencies:

  @capacitor/cli: 4.4.0
  @capacitor/core: 4.4.0
  @capacitor/android: 4.4.0
  @capacitor/ios: 4.4.0

[success] iOS looking great! 👌
[success] Android looking great! 👌


Platform(s)

Current Behavior

Requesting any URL that replies with set-cookie as a header seems to be ignored. document.cookie continues to be undefined.

Expected Behavior

With CapacitorHttp and CapacitorCookie enabled, requesting a URL that has a set-cookie should update document.cookie and send cookies afterwards to further requests.

Code Reproduction

Clone the repo here: https://github.com/silviogutierrez/capacitor-http-tests

This is virtually the result of npm init @capacitor/app but with these lines modified; https://github.com/silviogutierrez/capacitor-http-tests/blob/main/src/js/capacitor-welcome.js#L83

Run: npm install npm run build npx cap sync ios npx cap sync android npx open android npx open ios

Then run both projects. Click “make request” and see how the console logs cookies, and they continue to be undefined. Just in case it was an async thing, click “log cookies” a bit later to continue observing the behavior.

Note the curl for the URL being hit produces this (note the set-cookie header)

curl -X GET -I https://www.joyhealthtracker.dev/api/csrf/
HTTP/2 200
server: openresty/1.15.8.2
date: Thu, 27 Oct 2022 02:55:46 GMT
content-type: application/json
content-length: 2
strict-transport-security: max-age=15724800; includeSubDomains
vary: Cookie, Origin
x-frame-options: DENY
x-api-version: v13.0.0
set-cookie: csrftoken=vK5Kbn4cx4L2rGjKsw84QbMN6Blk5eq1yl378tLhQLnoKvkS2bt4up6wDRYd4fSC; expires=Thu, 26 Oct 2023 02:55:46 GMT; Max-Age=31449600; Path=/; SameSite=Lax
x-request-id: 1000f65fb8b46700f9bff000daf1be90

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
jrobesoncommented, Nov 17, 2022

@ItsChaceD then you can close this one right?

1reaction
jrobesoncommented, Nov 7, 2022

I have to do something like this to make document.cookies not be undefined, but i thought it might have worked otherwise. I"m not sure though

diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorCookieManager.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorCookieManager.java
index 9785318c..41847408 100644
--- a/android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorCookieManager.java
+++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorCookieManager.java
@@ -71,7 +71,7 @@ public class CapacitorCookieManager extends CookieManager {
         try {
             ArrayList<HttpCookie> cookieList = new ArrayList<>();
             String cookieString = getCookieString(url);
-            if (cookieString != null) {
+            if (cookieString != "") {
                 String[] singleCookie = cookieString.split(";");
                 for (String c : singleCookie) {
                     HttpCookie parsed = HttpCookie.parse(c).get(0);
diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorCookies.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorCookies.java
index e1989eac..59f7efbd 100644
--- a/android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorCookies.java
+++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorCookies.java
@@ -73,7 +73,11 @@ public class CapacitorCookies extends Plugin {
         try {
             String url = getServerUrl(null);
             if (!url.isEmpty()) {
-                return cookieManager.getCookieString(url);
+                String cookieResult = cookieManager.getCookieString(url);
+                if (cookieResult == null) {
+                    return "";
+                }
+                return cookieResult;
             }
         } catch (Exception e) {
             e.printStackTrace();

I’m not sure if both mods were required. I filed a bug somewhere about this, but I can’t seem to find it. I wonder if i failed to submit it properly 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

For some reason document.cookie not working - Stack Overflow
cookie does absolutely nothing. test-page.js is code which defines a custom-element class (extending LitElement - as are all the custom elements ...
Read more >
Capacitor Cookies Plugin API
The Capacitor Cookies API provides native cookie support via patching `document.cookie` to use native libraries.
Read more >
White screen/Load time: App launch takes > 30 seconds
I am currently facing an issue specifically on Android with Capacitor: The initial loading time of the app is over 30 seconds long....
Read more >
webpack referenceerror: module is not defined in es module scope ...
To treat it as a CommonJS script, rename it to use the '.cjs' file extension. ReferenceError: module is not defined in ES module...
Read more >
Changelog - Ionic 5 Full Starter App
It was not compatible with the latest version of Firebase v9 and when ... Firebase authentication using cookies is not working as expected....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found