HTTP
Cordova / Phonegap plugin for communicating with HTTP servers. Supports iOS and Android.
Advantages over Javascript requests:
- Background threading - all requests are done in a background thread
- SSL Pinning
Repo: https://github.com/silkimen/cordova-plugin-advanced-http
Installation
- Install the Cordova and Ionic Native plugins:
$ ionic cordova plugin add cordova-plugin-advanced-http $ npm install --save @ionic-native/http@4 - Add this plugin to your app's module
Supported platforms
- Android
- iOS
Usage
import { HTTP } from '@ionic-native/http';
constructor(private http: HTTP) {}
...
this.http.get('https://ionic.io', {}, {})
.then(data => {
console.log(data.status);
console.log(data.data); // data received by server
console.log(data.headers);
})
.catch(error => {
console.log(error.status);
console.log(error.error); // error message as string
console.log(error.headers);
});
Instance Members
getBasicAuthHeader(username, password)
This returns an object representing a basic HTTP Authorization header of the form.
| Param | Type | Details |
|---|---|---|
| username |
string
|
Username |
| password |
string
|
Password |
Returns: Object an object representing a basic HTTP Authorization header of the form {‘Authorization’: ‘Basic base64EncodedUsernameAndPassword’}
useBasicAuth(username, password)
This sets up all future requests to use Basic HTTP authentication with the given username and password.
| Param | Type | Details |
|---|---|---|
| username |
string
|
Username |
| password |
string
|
Password |
getHeaders(host)
Get all headers defined for a given hostname.
| Param | Type | Details |
|---|---|---|
| host |
string
|
The hostname |
Returns: string return all headers defined for the hostname
setHeader(host, header, value)
Set a header for all future requests. Takes a hostname, a header and a value.
| Param | Type | Details |
|---|---|---|
| host |
string
|
The hostname to be used for scoping this header |
| header |
string
|
The name of the header |
| value |
string
|
The value of the header |
getDataSerializer()
Get the name of the data serializer which will be used for all future POST and PUT requests.
Returns: string returns the name of the configured data serializer
setDataSerializer(serializer)
Set the data serializer which will be used for all future POST and PUT requests. Takes a string representing the name of the serializer.
| Param | Type | Details |
|---|---|---|
| serializer |
string
|
The name of the serializer. Can be urlencoded, utf8 or json |
setCookie(url, cookie)
Add a custom cookie.
| Param | Type | Details |
|---|---|---|
| url |
string
|
Scope of the cookie |
| cookie |
string
|
RFC compliant cookie string |
clearCookies()
Clear all cookies.
removeCookies(url, cb)
Remove cookies for given URL.
| Param | Type | Details |
|---|---|---|
| url |
string
|
|
| cb |
getCookieString(url)
Resolve cookie string for given URL.
| Param | Type | Details |
|---|---|---|
| url |
string
|
getRequestTimeout()
Get global request timeout value in seconds.
Returns: number returns the global request timeout value
setRequestTimeout(timeout)
Set global request timeout value in seconds.
| Param | Type | Details |
|---|---|---|
| timeout |
number
|
The timeout in seconds. Default 60 |
setSSLCertMode(mode)
Set SSL Cert handling mode, being one of the following values default: default SSL cert handling using system’s CA certs nocheck: disable SSL cert checking, trusting all certs (meant to be used only for testing purposes) pinned: trust only provided certs
| Param | Type | Details |
|---|---|---|
| mode |
'default'|'nocheck'|'pinned'
|
SSL Cert handling mode |
disableRedirect(disable)
Disable following redirects automatically.
| Param | Type | Details |
|---|---|---|
| disable |
boolean
|
Set to true to disable following redirects automatically |
Returns: Promise<void> returns a promise that will resolve on success, and reject on failure
post(url, body, headers)
Make a POST request
| Param | Type | Details |
|---|---|---|
| url |
string
|
The url to send the request to |
| body |
Object
|
The body of the request |
| headers |
Object
|
The headers to set for this request |
Returns: Promise<HTTPResponse> returns a promise that resolve on success, and reject on failure
get(url, parameters, headers)
Make a GET request
| Param | Type | Details |
|---|---|---|
| url |
string
|
The url to send the request to |
| parameters |
Object
|
Parameters to send with the request |
| headers |
Object
|
The headers to set for this request |
Returns: Promise<HTTPResponse> returns a promise that resolve on success, and reject on failure
put(url, body, headers)
Make a PUT request
| Param | Type | Details |
|---|---|---|
| url |
string
|
The url to send the request to |
| body |
Object
|
The body of the request |
| headers |
Object
|
The headers to set for this request |
Returns: Promise<HTTPResponse> returns a promise that resolve on success, and reject on failure
patch(url, body, headers)
Make a PATCH request
| Param | Type | Details |
|---|---|---|
| url |
string
|
The url to send the request to |
| body |
Object
|
The body of the request |
| headers |
Object
|
The headers to set for this request |
Returns: Promise<HTTPResponse> returns a promise that resolve on success, and reject on failure
delete(url, parameters, headers)
Make a DELETE request
| Param | Type | Details |
|---|---|---|
| url |
string
|
The url to send the request to |
| parameters |
Object
|
Parameters to send with the request |
| headers |
Object
|
The headers to set for this request |
Returns: Promise<HTTPResponse> returns a promise that resolve on success, and reject on failure
head(url, parameters, headers)
Make a HEAD request
| Param | Type | Details |
|---|---|---|
| url |
string
|
The url to send the request to |
| parameters |
Object
|
Parameters to send with the request |
| headers |
Object
|
The headers to set for this request |
Returns: Promise<HTTPResponse> returns a promise that resolve on success, and reject on failure
uploadFile(url, body, headers, filePath, name)
| Param | Type | Details |
|---|---|---|
| url |
string
|
The url to send the request to |
| body |
Object
|
The body of the request |
| headers |
Object
|
The headers to set for this request |
| filePath |
string
|
The local path of the file to upload |
| name |
string
|
The name of the parameter to pass the file along as |
Returns: Promise<any> returns a FileEntry promise that resolve on success, and reject on failure
downloadFile(url, body, headers, filePath)
| Param | Type | Details |
|---|---|---|
| url |
string
|
The url to send the request to |
| body |
Object
|
The body of the request |
| headers |
Object
|
The headers to set for this request |
| filePath |
string
|
The path to download the file to, including the file name. |
Returns: Promise<any> returns a FileEntry promise that resolve on success, and reject on failure
HTTPResponse
| Param | Type | Details |
|---|---|---|
| status |
number
|
The status number of the response |
| headers |
any
|
The headers of the response |
| url |
string
|
The URL of the response. This property will be the final URL obtained after any redirects. |
| data |
any
|
The data that is in the response. This property usually exists when a promise returned by a request method resolves. (optional) |
| error |
string
|
Error response from the server. This property usually exists when a promise returned by a request method rejects. (optional) |