CapacitorHttp
Capacitor Http API 通过修补 fetch 和 XMLHttpRequest 以使用原生库来提供原生 http 支持。它还提供了不使用 fetch 和 XMLHttpRequest 的原生 http 请求辅助方法。此插件与 @capacitor/core 捆绑在一起。
配置
默认情况下,window.fetch 和 XMLHttpRequest 的修补以使用原生库的功能是禁用的。
如果要启用此功能,请在 capacitor.config 文件中修改以下配置。
| Prop | Type | Description | Default |
|---|---|---|---|
enabled | boolean | 启用 fetch 和 XMLHttpRequest 的修补以使用原生库。 | false |
配置示例
在 capacitor.config.json 中:
{
"plugins": {
"CapacitorHttp": {
"enabled": true
}
}
}
在 capacitor.config.ts 中:
import { CapacitorConfig } from '@capacitor/cli';
const config: CapacitorConfig = {
plugins: {
CapacitorHttp: {
enabled: true,
},
},
};
export default config;
示例
使用修补的 fetch
const response = await fetch('https://example.com/data');
const data = await response.json();
使用辅助方法
import { CapacitorHttp } from '@capacitor/core';
const response = await CapacitorHttp.get({
url: 'https://example.com/data',
});
console.log(response.data);
POST 请求
import { CapacitorHttp } from '@capacitor/core';
const response = await CapacitorHttp.post({
url: 'https://example.com/data',
headers: {
'Content-Type': 'application/json',
},
data: {
key: 'value',
},
});
console.log(response.data);
API
get(...)
get(options: HttpOptions) => Promise<HttpResponse>
执行 GET 请求
| Param | Type |
|---|---|
options | |
Returns:
Promise<HttpResponse>
Since: 1.0.0
post(...)
post(options: HttpOptions) => Promise<HttpResponse>
执行 POST 请求
| Param | Type |
|---|---|
options | |
Returns:
Promise<HttpResponse>
Since: 1.0.0
put(...)
put(options: HttpOptions) => Promise<HttpResponse>
执行 PUT 请求
| Param | Type |
|---|---|
options | |
Returns:
Promise<HttpResponse>
Since: 1.0.0
patch(...)
patch(options: HttpOptions) => Promise<HttpResponse>
执行 PATCH 请求
| Param | Type |
|---|---|
options | |
Returns:
Promise<HttpResponse>
Since: 1.0.0
delete(...)
delete(options: HttpOptions) => Promise<HttpResponse>
执行 DELETE 请求
| Param | Type |
|---|---|
options | |
Returns:
Promise<HttpResponse>
Since: 1.0.0
request(...)
request(options: HttpOptions) => Promise<HttpResponse>
执行带有自定义方法或选项的请求
| Param | Type |
|---|---|
options | |
Returns:
Promise<HttpResponse>
Since: 1.0.0
Interfaces
HttpOptions
| Prop | Type | Description | Default | Since |
|---|---|---|---|---|
url | string | 请求的 URL | 1.0.0 | |
method | string | HTTP 方法 | 1.0.0 | |
data | any | FormData | 请求体数据 | 1.0.0 | |
headers | HttpHeaders | 请求头 | 1.0.0 | |
params | Record<string, string> | URL 查询参数 | 1.0.0 | |
connectTimeout | number | 连接超时(毫秒) | 1.0.0 | |
readTimeout | number | 读取超时(毫秒) | 1.0.0 | |
disableRedirects | boolean | 禁用自动重定向 | 1.0.0 | |
shouldEncode | boolean | 应该编码参数 | 1.0.0 | |
responseType | ResponseType | 响应类型 | 1.0.0 | |
webSecurityEnabled | boolean | 启用 Web 安全 | 1.0.0 |
HttpResponse
| Prop | Type | Description | Since |
|---|---|---|---|
data | any | 响应数据 | 1.0.0 |
status | number | HTTP 状态码 | 1.0.0 |
headers | HttpHeaders | 响应头 | 1.0.0 |
url | string | 最终 URL(重定向后) | 1.0.0 |
HttpHeaders
Record<string, string>
Type Aliases
ResponseType
'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream'