UNPKG

5.91 kBTypeScriptView Raw
1export interface HTTPResponse {
2 /**
3 * The status number of the response
4 */
5 status: number;
6 /**
7 * The data that is in the response. This property usually exists when a promise returned by a request method resolves.
8 */
9 data?: any;
10 /**
11 * The headers of the response
12 */
13 headers: any;
14 /**
15 * Error response from the server. This property usually exists when a promise returned by a request method rejects.
16 */
17 error?: string;
18}
19/**
20 * @name HTTP
21 * @description
22 * Cordova / Phonegap plugin for communicating with HTTP servers. Supports iOS and Android.
23 *
24 * Advantages over Javascript requests:
25 * - Background threading - all requests are done in a background thread
26 * - SSL Pinning
27 *
28 * @usage
29 * ```
30 * import { HTTP } from 'ionic-native';
31 *
32 * HTTP.get('http://ionic.io', {}, {})
33 * .then(data => {
34 *
35 * console.log(data.status);
36 * console.log(data.data); // data received by server
37 * console.log(data.headers);
38 *
39 * })
40 * .catch(error => {
41 *
42 * console.log(error.status);
43 * console.log(error.error); // error message as string
44 * console.log(error.headers);
45 *
46 * });
47 *
48 * ```
49 * @interfaces
50 * HTTPResponse
51 */
52export declare class HTTP {
53 /**
54 * This returns an object representing a basic HTTP Authorization header of the form.
55 * @param username {string} Username
56 * @param password {string} Password
57 * @returns {Object} an object representing a basic HTTP Authorization header of the form {'Authorization': 'Basic base64encodedusernameandpassword'}
58 */
59 static getBasicAuthHeader(username: string, password: string): {
60 Authorization: string;
61 };
62 /**
63 * This sets up all future requests to use Basic HTTP authentication with the given username and password.
64 * @param username {string} Username
65 * @param password {string} Password
66 */
67 static useBasicAuth(username: string, password: string): void;
68 /**
69 * Set a header for all future requests. Takes a header and a value.
70 * @param header {string} The name of the header
71 * @param value {string} The value of the header
72 */
73 static setHeader(header: string, value: string): void;
74 /**
75 * Enable or disable SSL Pinning. This defaults to false.
76 *
77 * To use SSL pinning you must include at least one .cer SSL certificate in your app project. You can pin to your server certificate or to one of the issuing CA certificates. For ios include your certificate in the root level of your bundle (just add the .cer file to your project/target at the root level). For android include your certificate in your project's platforms/android/assets folder. In both cases all .cer files found will be loaded automatically. If you only have a .pem certificate see this stackoverflow answer. You want to convert it to a DER encoded certificate with a .cer extension.
78 *
79 * As an alternative, you can store your .cer files in the www/certificates folder.
80 * @param enable {boolean} Set to true to enable
81 * @returns {Promise<void>} returns a promise that will resolve on success, and reject on failure
82 */
83 static enableSSLPinning(enable: boolean): Promise<void>;
84 /**
85 * Accept all SSL certificates. Or disabled accepting all certificates. Defaults to false.
86 * @param accept {boolean} Set to true to accept
87 * @returns {Promise<void>} returns a promise that will resolve on success, and reject on failure
88 */
89 static acceptAllCerts(accept: boolean): Promise<void>;
90 /**
91 * Whether or not to validate the domain name in the certificate. This defaults to true.
92 * @param validate {boolean} Set to true to validate
93 * @returns {Promise<void>} returns a promise that will resolve on success, and reject on failure
94 */
95 static validateDomainName(validate: boolean): Promise<void>;
96 /**
97 * Make a POST request
98 * @param url {string} The url to send the request to
99 * @param body {Object} The body of the request
100 * @param headers {Object} The headers to set for this request
101 * @returns {Promise<HTTPResponse>} returns a promise that resolve on success, and reject on failure
102 */
103 static post(url: string, body: any, headers: any): Promise<HTTPResponse>;
104 /**
105 *
106 * @param url {string} The url to send the request to
107 * @param parameters {Object} Parameters to send with the request
108 * @param headers {Object} The headers to set for this request
109 * @returns {Promise<HTTPResponse>} returns a promise that resolve on success, and reject on failure
110 */
111 static get(url: string, parameters: any, headers: any): Promise<HTTPResponse>;
112 /**
113 *
114 * @param url {string} The url to send the request to
115 * @param body {Object} The body of the request
116 * @param headers {Object} The headers to set for this request
117 * @param filePath {string} The local path of the file to upload
118 * @param name {string} The name of the parameter to pass the file along as
119 * @returns {Promise<HTTPResponse>} returns a promise that resolve on success, and reject on failure
120 */
121 static uploadFile(url: string, body: any, headers: any, filePath: string, name: string): Promise<HTTPResponse>;
122 /**
123 *
124 * @param url {string} The url to send the request to
125 * @param body {Object} The body of the request
126 * @param headers {Object} The headers to set for this request
127 * @param filePath {string} The path to donwload the file to, including the file name.
128 * @returns {Promise<HTTPResponse>} returns a promise that resolve on success, and reject on failure
129 */
130 static downloadFile(url: string, body: any, headers: any, filePath: string): Promise<HTTPResponse>;
131}