在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:capacitor-community/http开源软件地址:https://github.com/capacitor-community/http开源编程语言:Java 42.9%开源软件介绍:HTTP
Capacitor community plugin for native HTTP requests, file download/uploads, and cookie management. Maintainers
Installationnpm install @capacitor-community/http
npx cap sync Maintence ModeThe next iteration of this plugin will be an official plugin bundled with Capacitor 4.x. In order for a smooth transition, this repo will be in maintence mode with no new features added until the plugin moves to the main Capacitor Plugins repo. In the meantime, if there are critical security bug fixes required, they will still be made to this plugin as a patch release. Capacitor 2.xFor Capacitor 2.x projects, you will need to install a version less than 1.0.0. You can do that by specifying the version in your npm install @capacitor-community/[email protected] ConfigurationIn most cases no configuration is required for this plugin. If the Android application connects with use the self-signed certificates or without encryption, see Network security configuration article. UsageTo use the plugin while fully supporting the web version, import and use it like this: import { Http } from '@capacitor-community/http';
// Example of a GET request
const doGet = () => {
const options = {
url: 'https://example.com/my/api',
headers: { 'X-Fake-Header': 'Max was here' },
params: { size: 'XL' },
};
const response: HttpResponse = await Http.get(options);
// or...
// const response = await Http.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': 'Thomas was here' },
data: { foo: 'bar', cool: true },
};
const response: HttpResponse = await Http.post(options);
// or...
// const response = await Http.request({ ...options, method: 'POST' })
};
const setCookie = async () => {
const options = {
url: 'http://example.com',
key: 'language',
value: 'en',
};
await Http.setCookie(options);
};
const deleteCookie = async () => {
const options = {
url: 'http://example.com',
key: 'language',
};
await Http.deleteCookie(options);
};
const clearCookies = async () => {
await Http.clearCookies({ url: 'http://example.com' });
};
const getCookies = async () => {
const cookies: HttpCookie[] = await Http.getCookies({
url: 'http://example.com',
});
};
const downloadFile = async () => {
const options = {
url: 'https://example.com/path/to/download.pdf',
filePath: 'document.pdf',
fileDirectory: Directory.Downloads,
// Optional
method: 'GET',
};
// Writes to local filesystem
const response: HttpDownloadFileResult = await Http.downloadFile(options);
// Then read the file
if (response.path) {
const read = await Filesystem.readFile({
path: 'download.pdf',
directory: Directory.Downloads,
});
}
};
const uploadFile = async () => {
const options = {
url: 'https://example.com/path/to/upload.pdf',
name: 'myFile',
filePath: 'document.pdf',
fileDirectory: FilesystemDirectory.Downloads,
};
const response: HttpUploadFileResult = await Http.uploadFile();
}; API ReferenceYou can view the API Reference generated by TypeDoc here: https://capacitor-community.github.io/http/docs/classes/web.httpweb.html Third Party Cookies on iOSAs of iOS 14, you cannot use 3rd party cookies by default. There is an open issue on the Capacitor Core repo on properly patching in cookies on iOS. For now, you must specify a domain of for the cookie you are saving to properly save and send them via requests. You can also add the following lines to your <key>WKAppBoundDomains</key>
<array>
<string>www.mydomain.com</string>
<string>api.mydomain.com</string>
<string>www.myothercooldomain.com</string>
</array>
Contributors |
Daniel Sogl |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论