SetResponse on iOS requires NSDictionary but JS Sends String?
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.
+1.As a newer to RN, I don't know how to solve it...
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 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, () => {});
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
why not use response.headers.get('set-cookie')?
Oh sure, thats probably better
AFAIK, you can't read set-cookie headers from response. Fetch refuses to supply the header, if the cookie has a httpOnly setting.