cordova-plugin-advanced-http/README.md

292 lines
10 KiB
Markdown
Raw Normal View History

Cordova Advanced HTTP
=====================
[![npm version](https://badge.fury.io/js/cordova-plugin-advanced-http.svg)](https://badge.fury.io/js/cordova-plugin-advanced-http)
[![downloads/month](https://img.shields.io/npm/dm/cordova-plugin-advanced-http.svg)](https://www.npmjs.com/package/cordova-plugin-advanced-http)
[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.png)](https://opensource.org/licenses/mit-license.php)
2017-08-21 06:23:32 +08:00
[![Build Status](https://travis-ci.org/silkimen/cordova-plugin-advanced-http.svg?branch=master)](https://travis-ci.org/silkimen/cordova-plugin-advanced-http)
2014-03-05 04:48:24 +08:00
2014-04-01 08:14:50 +08:00
Cordova / Phonegap plugin for communicating with HTTP servers. Supports iOS and Android.
This is a fork of [Wymsee's Cordova-HTTP plugin](https://github.com/wymsee/cordova-HTTP).
2014-03-05 04:54:53 +08:00
2014-04-01 08:14:50 +08:00
## Advantages over Javascript requests
- Background threading - all requests are done in a background thread.
2016-11-14 21:43:24 +08:00
- Handling of HTTP code 401 - read more at [Issue CB-2415](https://issues.apache.org/jira/browse/CB-2415).
2014-04-01 08:14:50 +08:00
- SSL Pinning - read more at [LumberBlog](http://blog.lumberlabs.com/2012/04/why-app-developers-should-care-about.html).
2016-02-24 06:08:45 +08:00
## Updates
Please check [CHANGELOG.md](CHANGELOG.md) for details about updating to a new version.
2014-04-01 22:53:34 +08:00
## Installation
The plugin conforms to the Cordova plugin specification, it can be installed
using the Cordova / Phonegap command line interface.
```shell
phonegap plugin add cordova-plugin-advanced-http
2014-04-01 22:53:34 +08:00
cordova plugin add cordova-plugin-advanced-http
```
2014-04-01 08:14:50 +08:00
## Usage
2016-11-14 21:43:24 +08:00
### Without AngularJS
2016-11-14 22:13:26 +08:00
This plugin registers a global object located at `cordova.plugin.http`.
2016-11-14 21:43:24 +08:00
### With AngularJS
2014-04-01 08:14:50 +08:00
This plugin creates a cordovaHTTP service inside of a cordovaHTTP module. You must load the module when you create your app's module.
```js
var app = angular.module('myApp', ['ngRoute', 'ngAnimate', 'cordovaHTTP']);
```
2016-11-11 09:22:53 +08:00
2014-04-02 01:13:30 +08:00
You can then inject the cordovaHTTP service into your controllers. The functions can then be used identically to the examples shown below except that instead of accepting success and failure callback functions, each function returns a promise. For more information on promises in AngularJS read the [AngularJS docs](http://docs.angularjs.org/api/ng/service/$q). For more info on promises in general check out this article on [html5rocks](http://www.html5rocks.com/en/tutorials/es6/promises/). Make sure that you load cordova.js or phonegap.js after AngularJS is loaded.
2014-04-01 08:14:50 +08:00
2016-11-14 21:43:24 +08:00
## Synchronous Functions
2016-02-24 06:08:45 +08:00
### getBasicAuthHeader
This returns an object representing a basic HTTP Authorization header of the form `{'Authorization': 'Basic base64encodedusernameandpassword'}`
2014-04-01 08:14:50 +08:00
```js
var header = cordova.plugin.http.getBasicAuthHeader('user', 'password');
```
2014-04-01 08:14:50 +08:00
### useBasicAuth
This sets up all future requests to use Basic HTTP authentication with the given username and password.
```js
cordova.plugin.http.useBasicAuth('user', 'password');
```
2016-11-11 09:22:53 +08:00
2014-04-01 08:14:50 +08:00
### setHeader
2017-10-20 23:36:46 +08:00
Set a header for all future requests to a specified host. Takes a hostname, a header and a value.
2014-04-01 08:14:50 +08:00
```js
cordova.plugin.http.setHeader('Hostname', 'Header', 'Value');
```
2017-10-20 23:36:46 +08:00
You can also define headers used for all hosts by using wildcard character "\*" or providing only two params.
```js
cordova.plugin.http.setHeader('*', 'Header', 'Value');
cordova.plugin.http.setHeader('Header', 'Value');
```
The hostname also includes the port number. If you define a header for `www.example.com` it will not match following URL `http://www.example.com:8080`.
```js
// will match http://www.example.com/...
cordova.plugin.http.setHeader('www.example.com', 'Header', 'Value');
// will match http://www.example.com:8080/...
cordova.plugin.http.setHeader('www.example.com:8080', 'Header', 'Value');
```
2016-11-11 09:22:53 +08:00
### disableRedirect
If set to `true`, it won't follow redirects automatically. This is a global setting.
```js
cordova.plugin.http.disableRedirect(true);
```
### setDataSerializer
Set the data serializer which will be used for all future PATCH, POST and PUT requests. Takes a string representing the name of the serializer.
2016-11-11 09:22:53 +08:00
```js
cordova.plugin.http.setDataSerializer('urlencoded');
```
2016-11-11 09:22:53 +08:00
You can choose one of these two:
* `urlencoded`: send data as url encoded content in body (content type "application/x-www-form-urlencoded")
* `json`: send data as JSON encoded content in body (content type "application/json")
2016-02-24 06:08:45 +08:00
2016-11-14 21:43:24 +08:00
Caution: `urlencoded` does not support serializing deep structures whereas `json` does.
### setRequestTimeout
Set how long to wait for a request to respond, in seconds.
```js
cordova.plugin.http.setRequestTimeout(5.0);
```
2016-12-06 17:29:35 +08:00
### clearCookies
Clear the cookie store.
```js
cordova.plugin.http.clearCookies();
```
2016-11-14 21:43:24 +08:00
## Asynchronous Functions
2016-02-24 06:08:45 +08:00
These functions all take success and error callbacks as their last 2 arguments.
2014-04-01 08:14:50 +08:00
### enableSSLPinning
2016-02-24 06:08:45 +08:00
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](http://stackoverflow.com/a/16583429/3182729). You want to convert it to a DER encoded certificate with a .cer extension.
2014-04-01 08:14:50 +08:00
As an alternative, you can store your .cer files in the www/certificates folder.
```js
cordova.plugin.http.enableSSLPinning(true, function() {
console.log('success!');
}, function() {
console.log('error :(');
});
```
2016-11-11 09:22:53 +08:00
2014-04-01 08:14:50 +08:00
### acceptAllCerts
2016-02-24 06:08:45 +08:00
Accept all SSL certificates. Or disable accepting all certificates. This defaults to false.
2014-04-01 08:14:50 +08:00
```js
cordova.plugin.http.acceptAllCerts(true, function() {
console.log('success!');
}, function() {
console.log('error :(');
});
```
2016-11-11 09:22:53 +08:00
2016-02-24 06:08:45 +08:00
### validateDomainName
This function was removed in v1.6.2. Domain name validation is disabled automatically when you enable "acceptAllCerts".
2016-11-11 09:22:53 +08:00
### removeCookies
Remove all cookies associated with a given URL.
```js
cordova.plugin.http.removeCookies(url);
```
2014-04-01 08:29:56 +08:00
### post<a name="post"></a>
Execute a POST request. Takes a URL, data, and headers.
2014-04-01 08:14:50 +08:00
#### success
The success function receives a response object with 3 properties: status, data, and headers. **status** is the HTTP response code as numeric value. **data** is the response from the server as a string. **headers** is an object with the headers. The keys of the returned object are the header names and the values are the respective header values. All header names are lowercase.
Here's a quick example:
2014-04-01 08:14:50 +08:00
```js
{
status: 200,
data: '{"id": 12, "message": "test"}',
headers: {
'content-length': '247'
}
}
```
2016-11-11 09:22:53 +08:00
2014-04-01 08:14:50 +08:00
Most apis will return JSON meaning you'll want to parse the data like in the example below:
```js
cordova.plugin.http.post('https://google.com/', {
id: 12,
message: 'test'
}, { Authorization: 'OAuth2: token' }, function(response) {
// prints 200
console.log(response.status);
try {
response.data = JSON.parse(response.data);
// prints test
console.log(response.data.message);
} catch(e) {
console.error('JSON parsing error');
}
}, function(response) {
// prints 403
console.log(response.status);
//prints Permission denied
console.log(response.error);
});
```
2016-11-11 09:22:53 +08:00
2014-04-01 08:14:50 +08:00
#### failure
The error function receives a response object with 3 properties: status, error and headers. **status** is the HTTP response code as numeric value. **error** is the error response from the server as a string. **headers** is an object with the headers. The keys of the returned object are the header names and the values are the respective header values. All header names are lowercase.
Here's a quick example:
2014-04-01 08:14:50 +08:00
```js
{
status: 403,
error: 'Permission denied',
headers: {
'content-length': '247'
}
}
```
2016-11-11 09:22:53 +08:00
2014-04-01 08:14:50 +08:00
### get
Execute a GET request. Takes a URL, parameters, and headers. See the [post](#post) documentation for details on what is returned on success and failure.
2014-04-01 08:14:50 +08:00
```js
cordova.plugin.http.get('https://google.com/', {
id: 12,
message: 'test'
}, { Authorization: 'OAuth2: token' }, function(response) {
console.log(response.status);
}, function(response) {
console.error(response.error);
});
```
2016-11-11 09:22:53 +08:00
2016-11-14 21:43:24 +08:00
### put
Execute a PUT request. Takes a URL, data, and headers. See the [post](#post) documentation for details on what is returned on success and failure.
### patch
Execute a PATCH request. Takes a URL, data, and headers. See the [post](#post) documentation for details on what is returned on success and failure.
2016-11-14 21:43:24 +08:00
### delete
Execute a DELETE request. Takes a URL, parameters, and headers. See the [post](#post) documentation for details on what is returned on success and failure.
### head
Execute a HEAD request. Takes a URL, parameters, and headers. See the [post](#post) documentation for details on what is returned on success and failure.
2014-04-01 08:14:50 +08:00
### uploadFile
Uploads a file saved on the device. Takes a URL, parameters, headers, filePath, and the name of the parameter to pass the file along as. See the [post](#post) documentation for details on what is returned on success and failure.
2014-04-01 08:14:50 +08:00
```js
cordova.plugin.http.uploadFile("https://google.com/", {
id: 12,
message: 'test'
}, { Authorization: 'OAuth2: token' }, 'file:///somepicture.jpg', 'picture', function(response) {
console.log(response.status);
}, function(response) {
console.error(response.error);
});
```
2016-11-11 09:22:53 +08:00
2014-04-01 08:14:50 +08:00
### downloadFile
Downloads a file and saves it to the device. Takes a URL, parameters, headers, and a filePath. See [post](#post) documentation for details on what is returned on failure. On success this function returns a cordova [FileEntry object](http://cordova.apache.org/docs/en/3.3.0/cordova_file_file.md.html#FileEntry).
2014-04-01 08:14:50 +08:00
```js
cordova.plugin.http.downloadFile("https://google.com/", {
id: 12,
message: 'test'
}, { Authorization: 'OAuth2: token' }, 'file:///somepicture.jpg', function(entry) {
// prints the filename
console.log(entry.name);
// prints the filePath
console.log(entry.fullPath);
}, function(response) {
console.error(response.error);
});
```
2014-04-01 08:14:50 +08:00
## Libraries
2017-09-26 16:48:06 +08:00
This plugin utilizes some awesome open source networking libraries. These are both MIT licensed:
2014-04-01 08:14:50 +08:00
- iOS - [AFNetworking](https://github.com/AFNetworking/AFNetworking)
- Android - [http-request](https://github.com/kevinsawicki/http-request)
We made a few modifications to both of them.
2017-10-24 23:00:40 +08:00
## Contribute & Develop
We've set up a separate document for our [contribution guidelines](CONTRIBUTING.md).