wkh237 / react-native-fetch-blob

A project committed to making file access and data transfer easier, efficient for React Native developers.
MIT License
2.61k stars 1.59k forks source link

Android - send session cookies #220

Open Chandragandla opened 7 years ago

Chandragandla commented 7 years ago

Hi, I am using RN@0.34, RNFB@0.10.1-beta.3 and node/express.

Below is the code I use to send the uploaded document.

return RNFetchBlob.fetch('POST', nodeUrl, { 'Content-Type' : 'multipart/form-data', }, [{ name : 'file', filename : 'sample.png', data : wrappedURI }]) Session cookies are sent correctly for iOS , but for android, cookies are not being sent and user is getting logged out.

How can I inlcude credentials: 'include', with the request ?

Any help is greatly appreciated.

Thanks

Jacse commented 7 years ago

I need to be able to download documents from a "logged in" session on a web server. All is working well on iOS where the Cookies are sent, but it fails on Android. A fix would be much welcome.

wkh237 commented 7 years ago

Cookie support is still work in progress, and I think there is still a lot of problems need to be solved.

Besides, you can have a look at RNFetchBlob.net, and add cookies manually into headers.

Jacse commented 7 years ago

@wkh237 Cool, thanks. Can't quite get it to work with a specific host, but for now I'll just grab all hosts.

zuzuviewer commented 7 years ago

@wkh237 ,@Jacse,could you please tell me how to add cookies manually into headers.I add 'Cookie',but it didn't work

wkh237 commented 7 years ago

@zuzuviewer , it's not supported ATM, I'll add this one when I have time. Please have a look #157

Jacse commented 7 years ago

@zuzuviewer unfortunately I was only able to retrieve the cookies, not send them again. As you say, they are not sent in the request even when added as a header. @wkh237 let me know if I can do anything to speed things up, not very strong i Java though.

Jacse commented 7 years ago

I've done some testing and it seems that RNFetchBlob completely ignores the specified Cookie-header. It does send cookies, but only cookies that are set through RNFetchBlob-fetches, not regular fetches. This leads to having two different sets of cookies for RNFetchBlob and regular fetch.

Jacse commented 7 years ago

What I described in my previous post happens due to line #303 in RNFetchBlobReq.java: clientBuilder.cookieJar(new RNFBCookieJar()); As this sets a new custom cookie jar.

I got it all working by deleting said line and using facebooks OkHttpClientProvider.getOkHttpClient(); instead of initializing a new OkHttpClient. In this way, cookies set in a regular fetch persist across fetches in RNFetchBlob as well.

nikolaiwarner commented 7 years ago

@Jacse Do you have a branch going with that fix?

Jacse commented 7 years ago

@nikolaiwarner I do now: #388 👍

MEDLJosh commented 7 years ago

@Jacse I am having the same problem as the original person. I need to use credentials: 'include' to pass cookies to the API. Can you provide an example of how to do this with your merged code?

Jacse commented 7 years ago

@MEDLJosh it should work straight out of the box. If you use the regular fetch to get cookies, these cookies should be sent along with your react-native-fetch-blob request as well.

f6m6 commented 7 years ago

@Jacse is there a way to confirm this is happening clientside? cookie received with fetch is attaching fine to my fetch GET requests but not in react-native-fetch-blob it seems...

dominic3 commented 7 years ago

From looking at the code, I don't think that cookies are being sent if you use the Download Manager. The download manager sets up it's own request and doesn't make use of the okhttp3 client so that doesn't share the cookie jar. Would it be possible to grab the same-origin cookies from the cookie jar and inject them into the download request with it's addRequestHeader() method?

Ariel08081214 commented 6 years ago

any progress?

Jacse commented 6 years ago

@danna88 on what specifically?

yoginair commented 6 years ago

@Jacse Is download with credentials: 'include' working ? do you have work around for this?