-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug: CapacitorHTTP fetch with Request object fails #6174
Comments
Pull request #6175 |
This issue has been labeled as |
Can reproduce... I'm a newcomer to Capacitor, and this really has given me some headaches... |
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out. |
Bug Report
Capacitor Version
Platform(s)
Current Behavior
The Capacitor HTTP plugin supports a mode that patches the browser's window.fetch API when enabled.
With this active you can instead call the fetch API directly which will proxy all requests through the native Capacitor HTTP plugin on native platforms, i.e. Android, iOS. This is particularly useful to:
Http.request()
directly.However, the current implementation of this patch in the Capacitor core/native-bridge.ts does not take into account that the browser's fetch API can be called with multiple argument signatures.
The patch expects all request to come in the form of
fetch(resource: string, options: object)
where resource is the URL string of the request, while options is the request options like a body, headers, etc.The browser fetch API additionally allows for fetch requests to be made using a Request object instead, e.g.
fetch(resource: Request)
. In this case the patch will fallback to using the browser fetch API instead of the Capacitor native HTTP plugin due to this limited implementation.This is not the obvious behavior that a developer would expect. For example, making a request to a CORS protected resource with this 1st signature will work, while making a the request with the 2nd signature will fail.
To fix this, in core/NativeBridge.ts we can simply wrap the function arguments of the patch in a Request object to standardize further processing down stream across all possible function signatures.
Expected Behavior
With the fetch patch enabled, you should be able to call fetch using all browser supported signatures, e.g. including
fetch(resource: Request)
.Code Reproduction
See this Demo Repo that shows the issue.
npm run android
ornpm run ios
The demo contains 4 examples:
Capacitor.request
fetch(resource: string, options: object)
fetch(resource: Request)
See this Merge Request Repo that fixes the issue.
Other Technical Details
npm --version
output: 8.11.0node --version
output: v16.16.0pod --version
output (iOS issues only): 1.11.3Additional Context
n/a
The text was updated successfully, but these errors were encountered: