apache / cordova-plugin-inappbrowser

Apache Cordova InAppBrowser Plugin
https://cordova.apache.org/
Apache License 2.0
1.11k stars 2.14k forks source link

feat: add request headers and cookies as new options #1024

Open lubbo opened 7 months ago

lubbo commented 7 months ago

Platforms affected

Motivation and Context

It's currently not possible to set cookies and request headers when opening the IAB.

Description

I'm adding two new options:

Limitations

Testing

From JS side open the IAB with following options:

const headers = {
  Authorization: `Bearer exampleAccessToken`,
};
const headersString = JSON.stringify(headers);
//Needed because JS->Native parameters serialization uses = to key/value separator
const headersBase64 = btoa(headersString).replaceAll('=', '@');

const cookieOption: CookieSerializeOptions = {
        domain: 'www.mydomain.com',
        secure: true,
        path: '/',
        sameSite: 'strict',
      };

const serializedCookie = cookie.serialize(
        'TEST_NAME',
        'TEST_VALUE',
        cookieOption,
      );
const cookies = {
        ['https://www.mydomain.com']: serializedCookie,
      };
const cookiesString = JSON.stringify(cookies);
//Needed because JS->Native parameters serialization uses = to key/value separator
const cookiesBase64 = btoa(cookiesString).replaceAll('=', '@');

const options = {
  cookies: cookiesBase64,
  headers: headersBase64,
}
if (options && typeof options !== 'string') {
        options = Object.keys(options)
          .map((key: string) => `${key}=${(options as InAppBrowserOptions)[key]}`)
          .join(',');
      }

var ref = cordova.InAppBrowser.open('https://apache.org', '_blank', options);

Checklist

lubbo commented 7 months ago

Hi @janpio, can you take a look at this PR?

Crylion commented 2 months ago

Setting headers is a feature that I would also very much like to see :)

sbl05 commented 1 month ago

+1 to @Crylion This would be a really great addition! :)

marcellov7 commented 6 days ago

Is it possible to merge this?