Skip to content
This repository has been archived by the owner on Jan 24, 2020. It is now read-only.

SetResponse on iOS requires NSDictionary but JS Sends String? #15

Open
JesseARWhite opened this issue Mar 7, 2016 · 7 comments
Open

Comments

@JesseARWhite
Copy link

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.

@Kerwin92
Copy link

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

@rseemann
Copy link

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]]);
}

@jariz
Copy link

jariz commented Aug 29, 2016

@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, () => {});

@duhseekoh
Copy link

duhseekoh commented Jun 22, 2017

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

@jariz
Copy link

jariz commented Jun 22, 2017

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

@duhseekoh
Copy link

Oh sure, thats probably better

@mschipperheyn
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants