react-native-cookies icon indicating copy to clipboard operation
react-native-cookies copied to clipboard

SetResponse on iOS requires NSDictionary but JS Sends String?

Open JesseARWhite opened this issue 9 years ago • 7 comments

It appears that setResponse may be misaligned from the iOS Side to expect an NSDictionary when the documentation specifies to send a string for android. If this is the case can we either A) Line up the iOS side to accept a string and parse that into an NSDictionary for saving or B) Document the method on the iOS side as a noOP similar to what Set does on android.

JesseARWhite avatar Mar 07 '16 17:03 JesseARWhite

+1.As a newer to RN, I don't know how to solve it...

Kerwin92 avatar Mar 10 '16 13:03 Kerwin92

If you look at the method implementation it accepts only a dictionary, so we need to use the set method, not the setFromResponse

RCT_EXPORT_METHOD(setFromResponse:(NSURL *)url value:(NSDictionary *)value callback:(RCTResponseSenderBlock)callback) {
  NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:value forURL:url];
  [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:url mainDocumentURL:NULL];
    callback(@[[NSNull null]]);
}

rseemann avatar Apr 12 '16 23:04 rseemann

@rseemann That still doesn't explain why it differs from the Android version, which for the record, works fine. EDIT: seems like the following (bypass!) works. Doesn't take away the fact that this needs to be fixed. Considering the repo owner isn't accepting any PR's, it's not very motivating for me to submit one either.

let cookies = "Your cookie value string";

if(Platform.OS === "ios") {
    cookies = {
        "Set-Cookie": cookies
    };
}

CookieManager.setFromResponse("https://example.org", cookies, () => {});

jariz avatar Aug 29 '16 13:08 jariz

And if you're reading cookies from a fetch response

if(Platform.OS === "ios") {
  cookies = { "Set-Cookie": response.headers.get('set-cookie') };
} else {
  cookies = response.headers.get('set-cookie');
}

updated per @jariz comment below

duhseekoh avatar Jun 22 '17 21:06 duhseekoh

why not use response.headers.get('set-cookie')?

jariz avatar Jun 22 '17 21:06 jariz

Oh sure, thats probably better

duhseekoh avatar Jun 22 '17 21:06 duhseekoh

AFAIK, you can't read set-cookie headers from response. Fetch refuses to supply the header, if the cookie has a httpOnly setting.

mschipperheyn avatar Dec 04 '17 15:12 mschipperheyn