capacitor icon indicating copy to clipboard operation
capacitor copied to clipboard

[Feature]: Allow setting global headers for Capacitor Http

Open bosh-code opened this issue 1 year ago • 2 comments

Description

Add a section in the capactitor config for headers to be used by the capacitor Http plugin, similar to User Agent config.

Platforms

  • [X] iOS
  • [X] Android
  • [ ] Web

Request or proposed solution

I'm happy to do the changes for it if it something the maintainers would like to implement. I'm suggesting it as we have had to do it at work so requests to one of our APIs work.

In the capacitor configuration, something like this:

...
 plugins: {
    CapacitorHttp: {
      enabled: true,
      customHeaders: {
        Origin: 'example.com'
        ...
    },
...

Then in native-bridge:

convertBody(body).then(({ data, type, headers }) => {
  // Example based on the patch I wrote for our app:
  otherheaders = {
    ...customHeaders, // From cap config
    ...otherHeaders
  }
  ...

Alternatives

No response

Additional Information

No response

bosh-code avatar Apr 26 '24 02:04 bosh-code

Im also looking for a way to add a consistent Origin header in requests on android (not tested on ios yet) made from the foreground as well as the background threads. As far as I have observed, capacitor adds the server.hostname of the config as Origin, but only in get requests made by the foreground thread. For example, a post request delegated to the thread pool will not have an Origin header set at all (and unfortunately it will not pick the one defined in the fetch request options).

Have you experienced this and found a workaround for the origin header?

unre4l avatar Jun 12 '24 18:06 unre4l