7.0.0 โ€ข Published 10 months ago

@leadscout/http v7.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Forked Repo

This is a forked repo from @capacitor-mobi/http, which is itself a forked repo from @capacitor-community/http .

This fork adds a few updates:

  1. Update to Capacitor v7
  2. Added uploadImage API. This is similar to the existing uploadFile API but adds support for:
    • Resizing an image before upload
    • Adding custom metadata headers (width, height, size), which are all common for image uploads.
    • Support S3 uploads - the order of the form data matters for S3.
  3. Added upload queue for files and images to support controlled concurrency of uploads

Installation

npm install @leadscout/http
npx cap sync

Configuration

In 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.

Usage

To use the plugin while fully supporting the web version, import and use it like this:

import { Http } from '@leadscout/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();
};

Third Party Cookies on iOS

As 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 Info.plist file to get better support for cookies on iOS. You can add up to 10 domains.

<key>WKAppBoundDomains</key>
<array>
    <string>www.mydomain.com</string>
    <string>api.mydomain.com</string>
    <string>www.myothercooldomain.com</string>
</array>

Contributors โœจ

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

7.0.0

10 months ago

7.0.0-rc16

10 months ago

7.0.0-rc15

10 months ago

7.0.0-rc14

10 months ago

7.0.0-rc13

10 months ago

7.0.0-rc12

10 months ago

7.0.0-rc11

10 months ago

7.0.0-rc10

10 months ago

7.0.0-rc9

10 months ago

7.0.0-rc8

10 months ago

7.0.0-rc7

10 months ago

7.0.0-rc6

10 months ago

7.0.0-rc5

10 months ago

7.0.0-rc4

10 months ago

7.0.0-rc3

10 months ago

7.0.0-rc2

10 months ago

7.0.0-rc1

11 months ago

6.0.5-rc4

11 months ago

6.0.5-rc3

11 months ago

6.0.5-rc2

11 months ago

6.0.5-rc1

11 months ago

6.0.5

11 months ago

6.0.4

11 months ago

6.0.3

11 months ago

6.0.2

11 months ago

6.1.0

11 months ago

6.0.1

11 months ago

6.0.0

11 months ago