# angular-http-service

> An angular.js service for handling http/s requests.

Latest version **1.2.9** (published 2016-12-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install angular-http-service
pnpm add angular-http-service
yarn add angular-http-service
bun add angular-http-service
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.9 |
| Published | 2016-12-29 |
| First published | 2015-10-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 (+10 in 1 direct dependencies) |
| Install scripts | no |
| Author | David Poxon |
| Maintainers | davidpoxon |
| Keywords | angular, http, service |

## Links

- npm: https://www.npmjs.com/package/angular-http-service
- Repository: https://github.com/imagus/angular-http-service
- Issues: https://github.com/imagus/angular-http-service/issues
- npm.io page: https://npm.io/package/angular-http-service

## Dependencies (1)

- [angular](https://npm.io/package/angular.md) ^1.4.8

## Recent versions

- 1.2.9 (latest) — 2016-12-29
- 1.2.8 — 2016-12-29
- 1.2.7 — 2016-12-29
- 1.2.6 — 2016-01-21
- 1.2.5 — 2016-01-01
- 1.2.4 — 2015-12-21
- 1.2.3 — 2015-12-21
- 1.2.2 — 2015-12-21
- 1.2.1 — 2015-12-21
- 1.2.0 — 2015-12-21
- 1.1.0 — 2015-10-02
- 1.0.0 — 2015-10-02

## README

# angular-http-service

An angular.js service for handling http/s requests.

## Install

### NPM

```
npm install angular-http-service
```

### Bower

```
bower install angular-http-service
```

## Include

Add this script to your ```index.html```:

### NPM

```html
<script src="/node_modules/angular-http-service/angular-http-service.min.js"></script>
```

### Bower

```html
<script src="/bower_components/angular-http-service/src/angular-http-service.js"></script>
```

Inject the service:

```js
angular.module('app', ['service.http'])

.service('MyHttpService', function(HttpService) {
  ...
}
```

## Use

### Basic


Create a service
```js
.service('MyHttpService', function(HttpService) {
  var service = {
    getMyData: getMyData
  };

  return service;

  function getMyData() {
    return HttpService.handleRequest(HttpService.GET, 'http://api.example.com/data');
  }
}
```

Then consume the service
```js
.controller('DataCtrl', function($scope, MyHttpService) {
  $scope.getData = function() {
    MyHttpService.getMyData()
      .then(function(success) {
        console.log(success);
      }, function(error) {
        console.log(error)
      })
  };
}
```

### .handleRequest(method, url, withCredentials, parameters, options)

#### - method

This should be any one of the service methods, which are:

  * .GET
  * .POST
  * .PUT
  * .DELETE

#### - url

A string value of URL of the endpoint

#### - withCredentials (optional, default: false)

A boolean value, indicating that cookies should be included as part of the request - for use with CORS requests.

Further information on this option ([from here](http://www.html5rocks.com/en/tutorials/cors/)):

> The .withCredentials property will include any cookies from the remote domain in the request, and it will also set any cookies from the remote domain. Note that these cookies still honor same-origin policies, so your JavaScript code can’t access the cookies from document.cookie or the response headers. They can only be controlled by the remote domain.

#### - parameters (optional)

A JSON object representing the named parameters for the request, e.g.,:

```json
{
  "name": "john",
  "surname": "doe"
}
```

#### - options (optional)

A HttpOptions object.

### HttpOptions

HttpOptions is a collection of HttpOption objects. Use this to add http options to your request.

First, Add HttpOptions to your service

```js
.service('MyHttpService', function(HttpService, HttpOptions) {
  ...
}
```
  
To create a HttpOptions object:

```js
var options = new HttpOptions({
    data: { username: 'john', password: 'test123' },
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    transformRequest: function (obj) {
        var str = [];
        for (var p in obj)
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        return str.join("&");
    }
});
```

---
_Source: https://npm.io/package/angular-http-service · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
