CapacitorHttp
The Capacitor Http API provides native http support via patching fetch
and XMLHttpRequest
to use native libraries. It also provides helper methods for native http requests without the use of fetch
and XMLHttpRequest
. This plugin is bundled with @capacitor/core
.
Configuration
By default, the patching of window.fetch
and XMLHttpRequest
to use native libraries is disabled.
If you would like to enable this feature, modify the configuration below in the capacitor.config
file.
Prop | Type | Description | Default |
---|---|---|---|
enabled | boolean | Enable the patching of fetch and XMLHttpRequest to use native libraries instead. | false |
Example Configuration
In capacitor.config.json
:
{
"plugins": {
"CapacitorHttp": {
"enabled": true
}
}
}
In capacitor.config.ts
:
import { CapacitorConfig } from '@capacitor/cli';
const config: CapacitorConfig = {
plugins: {
CapacitorHttp: {
enabled: true,
},
},
};
export default config;
Example
import { CapacitorHttp } from '@capacitor/core';
// Example of a GET request
const doGet = () => {
const options = {
url: 'https://example.com/my/api',
headers: { 'X-Fake-Header': 'Fake-Value' },
params: { size: 'XL' },
};
const response: HttpResponse = await CapacitorHttp.get(options);
// or...
// const response = await CapacitorHttp.request({ ...options, method: 'GET' })
};
// Example of a POST request. Note: data
// can be passed as a raw JS Object (must be JSON serializable)
const doPost = () => {
const options = {
url: 'https://example.com/my/api',
headers: { 'X-Fake-Header': 'Fake-Value' },
data: { foo: 'bar' },
};
const response: HttpResponse = await CapacitorHttp.post(options);
// or...
// const response = await CapacitorHttp.request({ ...options, method: 'POST' })
};
Large File Support
Due to the nature of the bridge, parsing and transferring large amount of data from native to the web can cause issues. Support for downloading and uploading files to the native device is planned to be added to the @capacitor/filesystem
plugin in the near future. One way to potentially circumvent the issue of running out of memory in the meantime (specifically on Android) is to edit the AndroidManifest.xml
and add android:largeHeap="true"
to the application
element. Most apps should not need this and should instead focus on reducing their overall memory usage for improved performance. Enabling this also does not guarantee a fixed increase in available memory, because some devices are constrained by their total available memory.
API
request(...)
request(options: HttpOptions) => Promise<HttpResponse>
Make a Http Request to a server using native libraries.
Param | Type |
---|---|
options |
|
get(...)
get(options: HttpOptions) => Promise<HttpResponse>
Make a Http GET Request to a server using native libraries.
Param | Type |
---|---|
options |
|
post(...)
post(options: HttpOptions) => Promise<HttpResponse>
Make a Http POST Request to a server using native libraries.
Param | Type |
---|---|
options |
|
put(...)
put(options: HttpOptions) => Promise<HttpResponse>
Make a Http PUT Request to a server using native libraries.
Param | Type |
---|---|
options |
|
patch(...)
patch(options: HttpOptions) => Promise<HttpResponse>
Make a Http PATCH Request to a server using native libraries.
Param | Type |
---|---|
options |
|
delete(...)
delete(options: HttpOptions) => Promise<HttpResponse>
Make a Http DELETE Request to a server using native libraries.
Param | Type |
---|---|
options |
|