HTTP

Improve this doc

$ ionic plugin add cordova-plugin-http

Repo: https://github.com/wymsee/cordova-HTTP

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

Advantages over Javascript requests:

Supported platforms

Usage

import { HTTP } from 'ionic-native';

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);

  });

Static 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

setHeader(header, value)

Set a header for all future requests. Takes a header and a value.

Param Type Details
header string

The name of the header

value string

The value of the header

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

validateDomainName(validate)

Whether or not to validate the domain name in the certificate. This defaults to true.

Param Type Details
validate boolean

Set to true to validate

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)

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

data
(optional)
any

The data that is in the response. This property usually exists when a promise returned by a request method resolves.

headers any

The headers of the response

error
(optional)
string

Error response from the server. This property usually exists when a promise returned by a request method rejects.

API

Native

General