UNPKG

20.1 kBTypeScriptView Raw
1import { IonicNativePlugin } from '@ionic-native/core';
2export interface HTTPResponse {
3 /**
4 * The HTTP status number of the response or a negative internal error code.
5 */
6 status: number;
7 /**
8 * The headers of the response.
9 */
10 headers: {
11 [key: string]: string;
12 };
13 /**
14 * The URL of the response. This property will be the final URL obtained after any redirects.
15 */
16 url: string;
17 /**
18 * The data that is in the response. This property usually exists when a promise returned by a request method resolves.
19 */
20 data?: any;
21 /**
22 * Error response from the server. This property usually exists when a promise returned by a request method rejects.
23 */
24 error?: string;
25}
26interface AbortedResponse {
27 aborted: boolean;
28}
29/**
30 * @name HTTP
31 * @description
32 * Cordova / Phonegap plugin for communicating with HTTP servers. Supports iOS and Android.
33 *
34 * Advantages over Javascript requests:
35 * - SSL / TLS Pinning
36 * - CORS restrictions do not apply
37 * - Handling of HTTP code 401 - read more at [Issue CB-2415](https://issues.apache.org/jira/browse/CB-2415)
38 *
39 * @usage
40 * ```typescript
41 * import { HTTP } from '@ionic-native/http/ngx';
42 *
43 * constructor(private http: HTTP) {}
44 *
45 * ...
46 *
47 * this.http.get('http://ionic.io', {}, {})
48 * .then(data => {
49 *
50 * console.log(data.status);
51 * console.log(data.data); // data received by server
52 * console.log(data.headers);
53 *
54 * })
55 * .catch(error => {
56 *
57 * console.log(error.status);
58 * console.log(error.error); // error message as string
59 * console.log(error.headers);
60 *
61 * });
62 *
63 * ```
64 * @interfaces
65 * HTTPResponse
66 */
67export declare class HTTPOriginal extends IonicNativePlugin {
68 /**
69 * This enum represents the internal error codes which can be returned in a HTTPResponse object.
70 * @readonly
71 */
72 readonly ErrorCode: {
73 GENERIC: number;
74 SSL_EXCEPTION: number;
75 SERVER_NOT_FOUND: number;
76 TIMEOUT: number;
77 UNSUPPORTED_URL: number;
78 NOT_CONNECTED: number;
79 POST_PROCESSING_FAILED: number;
80 ABORTED: number;
81 };
82 /**
83 * This returns an object representing a basic HTTP Authorization header of the form.
84 * @param username {string} Username
85 * @param password {string} Password
86 * @returns {Object} an object representing a basic HTTP Authorization header of the form {'Authorization': 'Basic base64EncodedUsernameAndPassword'}
87 */
88 getBasicAuthHeader(username: string, password: string): {
89 Authorization: string;
90 };
91 /**
92 * This sets up all future requests to use Basic HTTP authentication with the given username and password.
93 * @param username {string} Username
94 * @param password {string} Password
95 */
96 useBasicAuth(username: string, password: string): void;
97 /**
98 * Get all headers defined for a given hostname.
99 * @param host {string} The hostname
100 * @returns {string} return all headers defined for the hostname
101 */
102 getHeaders(host: string): string;
103 /**
104 * Set a header for all future requests. Takes a hostname, a header and a value.
105 * @param host {string} The hostname to be used for scoping this header
106 * @param header {string} The name of the header
107 * @param value {string} The value of the header
108 */
109 setHeader(host: string, header: string, value: string): void;
110 /**
111 * Get the name of the data serializer which will be used for all future POST and PUT requests.
112 * @returns {string} returns the name of the configured data serializer
113 */
114 getDataSerializer(): string;
115 /**
116 * Set the data serializer which will be used for all future POST, PUT and PATCH requests. Takes a string representing the name of the serializer.
117 * @param serializer {string} The name of the serializer.
118 * @see https://github.com/silkimen/cordova-plugin-advanced-http#setdataserializer
119 */
120 setDataSerializer(serializer: 'urlencoded' | 'json' | 'utf8' | 'multipart' | 'raw'): void;
121 /**
122 * Add a custom cookie.
123 * @param url {string} Scope of the cookie
124 * @param cookie {string} RFC compliant cookie string
125 */
126 setCookie(url: string, cookie: string): void;
127 /**
128 * Clear all cookies.
129 */
130 clearCookies(): void;
131 /**
132 * Remove cookies for given URL.
133 * @param url {string}
134 * @param cb
135 */
136 removeCookies(url: string, cb: () => void): void;
137 /**
138 * Resolve cookie string for given URL.
139 * @param url {string}
140 */
141 getCookieString(url: string): string;
142 /**
143 * Get global request timeout value in seconds.
144 * @returns {number} returns the global request timeout value
145 */
146 getRequestTimeout(): number;
147 /**
148 * Set global request timeout value in seconds.
149 * @param timeout {number} The timeout in seconds. Default 60
150 */
151 setRequestTimeout(timeout: number): void;
152 /**
153 * Resolve if it should follow redirects automatically.
154 * @returns {boolean} returns true if it is configured to follow redirects automatically
155 */
156 getFollowRedirect(): boolean;
157 /**
158 * Configure if it should follow redirects automatically.
159 * @param follow {boolean} Set to false to disable following redirects automatically
160 */
161 setFollowRedirect(follow: boolean): void;
162 /**
163 * Set server trust mode, being one of the following values:
164 * default: default SSL trustship and hostname verification handling using system's CA certs;
165 * legacy: use legacy default behavior (< 2.0.3), excluding user installed CA certs (only for Android);
166 * nocheck: disable SSL certificate checking and hostname verification, trusting all certs (meant to be used only for testing purposes);
167 * pinned: trust only provided certificates;
168 * @see https://github.com/silkimen/cordova-plugin-advanced-http#setservertrustmode
169 * @param {string} mode server trust mode
170 */
171 setServerTrustMode(mode: 'default' | 'legacy' | 'nocheck' | 'pinned'): Promise<void>;
172 /**
173 * Make a POST request
174 * @param url {string} The url to send the request to
175 * @param body {Object} The body of the request
176 * @param headers {Object} The headers to set for this request
177 * @returns {Promise<HTTPResponse>} returns a promise that will resolve on success, and reject on failure
178 */
179 post(url: string, body: any, headers: any): Promise<HTTPResponse>;
180 /**
181 * Make a sync POST request
182 * @param url {string} The url to send the request to
183 * @param body {Object} The body of the request
184 * @param headers {Object} The headers to set for this request
185 * @param success {function} A callback that is called when the request succeed
186 * @param failure {function} A callback that is called when the request failed
187 * @returns {string} returns a string that represents the requestId
188 */
189 postSync(url: string, body: any, headers: any, success: (result: HTTPResponse) => void, failure: (error: any) => void): string;
190 /**
191 * Make a GET request
192 * @param url {string} The url to send the request to
193 * @param parameters {Object} Parameters to send with the request
194 * @param headers {Object} The headers to set for this request
195 * @returns {Promise<HTTPResponse>} returns a promise that will resolve on success, and reject on failure
196 */
197 get(url: string, parameters: any, headers: any): Promise<HTTPResponse>;
198 /**
199 * Make a sync GET request
200 * @param url {string} The url to send the request to
201 * @param parameters {Object} Parameters to send with the request
202 * @param headers {Object} The headers to set for this request
203 * @param success {function} A callback that is called when the request succeed
204 * @param failure {function} A callback that is called when the request failed
205 * @returns {string} returns a string that represents the requestId
206 */
207 getSync(url: string, parameters: any, headers: any, success: (result: HTTPResponse) => void, failure: (error: any) => void): string;
208 /**
209 * Make a PUT request
210 * @param url {string} The url to send the request to
211 * @param body {Object} The body of the request
212 * @param headers {Object} The headers to set for this request
213 * @returns {Promise<HTTPResponse>} returns a promise that will resolve on success, and reject on failure
214 */
215 put(url: string, body: any, headers: any): Promise<HTTPResponse>;
216 /**
217 * Make a sync PUT request
218 * @param url {string} The url to send the request to
219 * @param body {Object} The body of the request
220 * @param headers {Object} The headers to set for this request
221 * @param success {function} A callback that is called when the request succeed
222 * @param failure {function} A callback that is called when the request failed
223 * @returns {string} returns a string that represents the requestId
224 */
225 putSync(url: string, body: any, headers: any, success: (result: HTTPResponse) => void, failure: (error: any) => void): string;
226 /**
227 * Make a PATCH request
228 * @param url {string} The url to send the request to
229 * @param body {Object} The body of the request
230 * @param headers {Object} The headers to set for this request
231 * @returns {Promise<HTTPResponse>} returns a promise that will resolve on success, and reject on failure
232 */
233 patch(url: string, body: any, headers: any): Promise<HTTPResponse>;
234 /**
235 * Make a sync PATCH request
236 * @param url {string} The url to send the request to
237 * @param body {Object} The body of the request
238 * @param headers {Object} The headers to set for this request
239 * @param success {function} A callback that is called when the request succeed
240 * @param failure {function} A callback that is called when the request failed
241 * @returns {string} returns a string that represents the requestId
242 */
243 patchSync(url: string, body: any, headers: any, success: (result: HTTPResponse) => void, failure: (error: any) => void): string;
244 /**
245 * Make a DELETE request
246 * @param url {string} The url to send the request to
247 * @param parameters {Object} Parameters to send with the request
248 * @param headers {Object} The headers to set for this request
249 * @returns {Promise<HTTPResponse>} returns a promise that will resolve on success, and reject on failure
250 */
251 delete(url: string, parameters: any, headers: any): Promise<HTTPResponse>;
252 /**
253 * Make a sync DELETE request
254 * @param url {string} The url to send the request to
255 * @param parameters {Object} Parameters to send with the request
256 * @param headers {Object} The headers to set for this request
257 * @param success {function} A callback that is called when the request succeed
258 * @param failure {function} A callback that is called when the request failed
259 * @returns {string} returns a string that represents the requestId
260 */
261 deleteSync(url: string, parameters: any, headers: any, success: (result: HTTPResponse) => void, failure: (error: any) => void): string;
262 /**
263 * Make a HEAD request
264 * @param url {string} The url to send the request to
265 * @param parameters {Object} Parameters to send with the request
266 * @param headers {Object} The headers to set for this request
267 * @returns {Promise<HTTPResponse>} returns a promise that will resolve on success, and reject on failure
268 */
269 head(url: string, parameters: any, headers: any): Promise<HTTPResponse>;
270 /**
271 * Make a sync HEAD request
272 * @param url {string} The url to send the request to
273 * @param parameters {Object} Parameters to send with the request
274 * @param headers {Object} The headers to set for this request
275 * @param success {function} A callback that is called when the request succeed
276 * @param failure {function} A callback that is called when the request failed
277 * @returns {string} returns a string that represents the requestId
278 */
279 headSync(url: string, parameters: any, headers: any, success: (result: HTTPResponse) => void, failure: (error: any) => void): string;
280 /**
281 * Make an OPTIONS request
282 * @param url {string} The url to send the request to
283 * @param parameters {Object} Parameters to send with the request
284 * @param headers {Object} The headers to set for this request
285 * @returns {Promise<HTTPResponse>} returns a promise that will resolve on success, and reject on failure
286 */
287 options(url: string, parameters: any, headers: any): Promise<HTTPResponse>;
288 /**
289 * Make an sync OPTIONS request
290 * @param url {string} The url to send the request to
291 * @param parameters {Object} Parameters to send with the request
292 * @param headers {Object} The headers to set for this request
293 * @param success {function} A callback that is called when the request succeed
294 * @param failure {function} A callback that is called when the request failed
295 * @returns {string} returns a string that represents the requestId
296 */
297 optionsSync(url: string, parameters: any, headers: any, success: (result: HTTPResponse) => void, failure: (error: any) => void): string;
298 /**
299 *
300 * @param url {string} The url to send the request to
301 * @param body {Object} The body of the request
302 * @param headers {Object} The headers to set for this request
303 * @param filePath {string} The local path(s) of the file(s) to upload
304 * @param name {string} The name(s) of the parameter to pass the file(s) along as
305 * @returns {Promise<any>} returns a FileEntry promise that will resolve on success, and reject on failure
306 */
307 uploadFile(url: string, body: any, headers: any, filePath: string | string[], name: string | string[]): Promise<any>;
308 /**
309 *
310 * @param url {string} The url to send the request to
311 * @param body {Object} The body of the request
312 * @param headers {Object} The headers to set for this request
313 * @param filePath {string} The local path(s) of the file(s) to upload
314 * @param name {string} The name(s) of the parameter to pass the file(s) along as
315 * @param success {function} A callback that is called when the request succeed
316 * @param failure {function} A callback that is called when the request failed
317 * @returns {string} returns a string that represents the requestId
318 */
319 uploadFileSync(url: string, body: any, headers: any, filePath: string | string[], name: string | string[], success: (result: any) => void, failure: (error: any) => void): string;
320 /**
321 *
322 * @param url {string} The url to send the request to
323 * @param body {Object} The body of the request
324 * @param headers {Object} The headers to set for this request
325 * @param filePath {string} The path to download the file to, including the file name.
326 * @returns {Promise<any>} returns a FileEntry promise that will resolve on success, and reject on failure
327 */
328 downloadFile(url: string, body: any, headers: any, filePath: string): Promise<any>;
329 /**
330 *
331 * @param url {string} The url to send the request to
332 * @param body {Object} The body of the request
333 * @param headers {Object} The headers to set for this request
334 * @param filePath {string} The path to download the file to, including the file name.
335 * @param success {function} A callback that is called when the request succeed
336 * @param failure {function} A callback that is called when the request failed
337 * @returns {string} returns a string that represents the requestId
338 */
339 downloadFileSync(url: string, body: any, headers: any, filePath: string, success: (result: any) => void, failure: (error: any) => void): string;
340 /**
341 *
342 * @param url {string} The url to send the request to
343 * @param options {Object} options for individual request
344 * @param options.method {string} request method
345 * @param options.data {Object} payload to be send to the server (only applicable on post, put or patch methods)
346 * @param options.params {Object} query params to be appended to the URL (only applicable on get, head, delete, upload or download methods)
347 * @param options.serializer {string} data serializer to be used (only applicable on post, put or patch methods), defaults to global serializer value, see setDataSerializer for supported values
348 * @param options.timeout {number} timeout value for the request in seconds, defaults to global timeout value
349 * @param options.headers {Object} headers object (key value pair), will be merged with global values
350 * @param options.filePath {string} file path(s) to be used during upload and download see uploadFile and downloadFile for detailed information
351 * @param options.name {string} name(s) to be used during upload see uploadFile for detailed information
352 * @param options.responseType {string} response type, defaults to text
353 *
354 * @returns {Promise<HTTPResponse>} returns a promise that will resolve on success, and reject on failure
355 */
356 sendRequest(url: string, options: {
357 method: 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete' | 'options' | 'upload' | 'download';
358 data?: {
359 [index: string]: any;
360 };
361 params?: {
362 [index: string]: string | number;
363 };
364 serializer?: 'json' | 'urlencoded' | 'utf8' | 'multipart' | 'raw';
365 timeout?: number;
366 headers?: {
367 [index: string]: string;
368 };
369 filePath?: string | string[];
370 name?: string | string[];
371 responseType?: 'text' | 'arraybuffer' | 'blob' | 'json';
372 }): Promise<HTTPResponse>;
373 /**
374 *
375 * @param url {string} The url to send the request to
376 * @param options {Object} options for individual request
377 * @param options.method {string} request method
378 * @param options.data {Object} payload to be send to the server (only applicable on post, put or patch methods)
379 * @param options.params {Object} query params to be appended to the URL (only applicable on get, head, delete, upload or download methods)
380 * @param options.serializer {string} data serializer to be used (only applicable on post, put or patch methods), defaults to global serializer value, see setDataSerializer for supported values
381 * @param options.timeout {number} timeout value for the request in seconds, defaults to global timeout value
382 * @param options.headers {Object} headers object (key value pair), will be merged with global values
383 * @param options.filePath {string} file path(s) to be used during upload and download see uploadFile and downloadFile for detailed information
384 * @param options.name {string} name(s) to be used during upload see uploadFile for detailed information
385 * @param options.responseType {string} response type, defaults to text
386 * @param success {function} A callback that is called when the request succeed
387 * @param failure {function} A callback that is called when the request failed
388 *
389 * @returns {string} returns a string that represents the requestId
390 */
391 sendRequestSync(url: string, options: {
392 method: 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete' | 'options' | 'upload' | 'download';
393 data?: {
394 [index: string]: any;
395 };
396 params?: {
397 [index: string]: string | number;
398 };
399 serializer?: 'json' | 'urlencoded' | 'utf8' | 'multipart';
400 timeout?: number;
401 headers?: {
402 [index: string]: string;
403 };
404 filePath?: string | string[];
405 name?: string | string[];
406 responseType?: 'text' | 'arraybuffer' | 'blob' | 'json';
407 }, success: (result: HTTPResponse) => void, failure: (error: any) => void): string;
408 /**
409 * @param requestId {string} The RequestId of the request to abort
410 */
411 abort(requestId: string): Promise<AbortedResponse>;
412}
413export {};
414
415export declare const HTTP: HTTPOriginal;
\No newline at end of file