Files
archived-tauri-docs/docs/api/js/classes/http.Client.md
2022-07-15 12:00:05 +02:00

5.1 KiB

@tauri-apps/api / http / Client

Class: Client

http.Client

Properties

id

id: number

Defined in

http.ts:251

Methods

delete

delete<T>(url, options?): Promise<Response<T>>

Makes a DELETE request.

Example

import { getClient } from '@tauri-apps/api/http';
const client = await getClient();
const response = await client.delete('http://localhost:3003/users/1');

Type parameters

Name
T

Parameters

Name Type Description
url string The request URL.
options? RequestOptions The request options.

Returns

Promise<Response<T>>

A promise resolving to the response.


drop

drop(): Promise<void>

Drops the client instance.

Example

import { getClient } from '@tauri-apps/api/http';
const client = await getClient();
await client.drop();

Returns

Promise<void>


get

get<T>(url, options?): Promise<Response<T>>

Makes a GET request.

Example

import { getClient, ResponseType } from '@tauri-apps/api/http';
const client = await getClient();
const response = await client.get('http://localhost:3003/users', {
  timeout: 30,
  // the expected response type
  responseType: ResponseType.JSON
});

Type parameters

Name
T

Parameters

Name Type Description
url string The request URL.
options? RequestOptions The request options.

Returns

Promise<Response<T>>

A promise resolving to the response.


patch

patch<T>(url, options?): Promise<Response<T>>

Makes a PATCH request.

Example

import { getClient, Body } from '@tauri-apps/api/http';
const client = await getClient();
const response = await client.patch('http://localhost:3003/users/1', {
  body: Body.json({ email: 'contact@tauri.app' })
});

Type parameters

Name
T

Parameters

Name Type Description
url string The request URL.
options? RequestOptions The request options.

Returns

Promise<Response<T>>

A promise resolving to the response.


post

post<T>(url, body?, options?): Promise<Response<T>>

Makes a POST request.

Example

import { getClient, Body, ResponseType } from '@tauri-apps/api/http';
const client = await getClient();
const response = await client.post('http://localhost:3003/users', {
  body: Body.json({
    name: 'tauri',
    password: 'awesome'
  }),
  // in this case the server returns a simple string
  responseType: ResponseType.Text,
});

Type parameters

Name
T

Parameters

Name Type Description
url string The request URL.
body? Body The body of the request.
options? RequestOptions The request options.

Returns

Promise<Response<T>>

A promise resolving to the response.


put

put<T>(url, body?, options?): Promise<Response<T>>

Makes a PUT request.

Example

import { getClient, Body } from '@tauri-apps/api/http';
const client = await getClient();
const response = await client.put('http://localhost:3003/users/1', {
  body: Body.form({
    file: {
      file: '/home/tauri/avatar.png',
      mime: 'image/png',
      fileName: 'avatar.png'
    }
  })
});

Type parameters

Name
T

Parameters

Name Type Description
url string The request URL.
body? Body The body of the request.
options? RequestOptions Request options.

Returns

Promise<Response<T>>

A promise resolving to the response.


request

request<T>(options): Promise<Response<T>>

Makes an HTTP request.

Example

import { getClient } from '@tauri-apps/api/http';
const client = await getClient();
const response = await client.request({
  method: 'GET',
  url: 'http://localhost:3003/users',
});

Type parameters

Name
T

Parameters

Name Type Description
options HttpOptions The request options.

Returns

Promise<Response<T>>

A promise resolving to the response.