HTTP

Improve this doc

Cordova / Phonegap plugin for communicating with HTTP servers. Supports iOS and Android.

Advantages over Javascript requests:

Repo: https://github.com/silkimen/cordova-plugin-advanced-http

Installation

  1. Install the Cordova and Ionic Native plugins:
    $ ionic cordova plugin add cordova-plugin-advanced-http
    $ npm install --save @ionic-native/http
    
  2. Add this plugin to your app's module

Supported platforms

Usage

import { HTTP } from '@ionic-native/http';

constructor(private http: HTTP) {}

...

this.http.get('http://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

enableSSLPinning(enable)

Enable or disable SSL Pinning. This defaults to false.

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.

As an alternative, you can store your .cer files in the www/certificates folder.

Param Type Details
enable boolean

Set to true to enable

Returns: Promise<void> returns a promise that will resolve on success, and reject on failure

acceptAllCerts(accept)

Accept all SSL certificates. Or disabled accepting all certificates. Defaults to false.

Param Type Details
accept boolean

Set to true to accept

Returns: Promise<void> returns a promise that will resolve on success, and reject on failure

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<HTTPResponse> returns a 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 donwload the file to, including the file name.

Returns: Promise<HTTPResponse> returns a 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)

API

Native

General