Project

General

Profile

1
import { Observable } from 'rxjs/Observable';
2
import { RequestOptions } from './base_request_options';
3
import { ConnectionBackend, RequestOptionsArgs } from './interfaces';
4
import { Request } from './static_request';
5
import { Response } from './static_response';
6
/**
7
 * Performs http requests using `XMLHttpRequest` as the default backend.
8
 *
9
 * `Http` is available as an injectable class, with methods to perform http requests. Calling
10
 * `request` returns an `Observable` which will emit a single {@link Response} when a
11
 * response is received.
12
 *
13
 * ### Example
14
 *
15
 * ```typescript
16
 * import {Http, HTTP_PROVIDERS} from '@angular/http';
17
 * import 'rxjs/add/operator/map'
18
 * @Component({
19
 *   selector: 'http-app',
20
 *   viewProviders: [HTTP_PROVIDERS],
21
 *   templateUrl: 'people.html'
22
 * })
23
 * class PeopleComponent {
24
 *   constructor(http: Http) {
25
 *     http.get('people.json')
26
 *       // Call map on the response observable to get the parsed people object
27
 *       .map(res => res.json())
28
 *       // Subscribe to the observable to get the parsed people object and attach it to the
29
 *       // component
30
 *       .subscribe(people => this.people = people);
31
 *   }
32
 * }
33
 * ```
34
 *
35
 *
36
 * ### Example
37
 *
38
 * ```
39
 * http.get('people.json').subscribe((res:Response) => this.people = res.json());
40
 * ```
41
 *
42
 * The default construct used to perform requests, `XMLHttpRequest`, is abstracted as a "Backend" (
43
 * {@link XHRBackend} in this case), which could be mocked with dependency injection by replacing
44
 * the {@link XHRBackend} provider, as in the following example:
45
 *
46
 * ### Example
47
 *
48
 * ```typescript
49
 * import {BaseRequestOptions, Http} from '@angular/http';
50
 * import {MockBackend} from '@angular/http/testing';
51
 * var injector = Injector.resolveAndCreate([
52
 *   BaseRequestOptions,
53
 *   MockBackend,
54
 *   {provide: Http, useFactory:
55
 *       function(backend, defaultOptions) {
56
 *         return new Http(backend, defaultOptions);
57
 *       },
58
 *       deps: [MockBackend, BaseRequestOptions]}
59
 * ]);
60
 * var http = injector.get(Http);
61
 * http.get('request-from-mock-backend.json').subscribe((res:Response) => doSomething(res));
62
 * ```
63
 *
64
 * @experimental
65
 */
66
export declare class Http {
67
    protected _backend: ConnectionBackend;
68
    protected _defaultOptions: RequestOptions;
69
    constructor(_backend: ConnectionBackend, _defaultOptions: RequestOptions);
70
    /**
71
     * Performs any type of http request. First argument is required, and can either be a url or
72
     * a {@link Request} instance. If the first argument is a url, an optional {@link RequestOptions}
73
     * object can be provided as the 2nd argument. The options object will be merged with the values
74
     * of {@link BaseRequestOptions} before performing the request.
75
     */
76
    request(url: string | Request, options?: RequestOptionsArgs): Observable<Response>;
77
    /**
78
     * Performs a request with `get` http method.
79
     */
80
    get(url: string, options?: RequestOptionsArgs): Observable<Response>;
81
    /**
82
     * Performs a request with `post` http method.
83
     */
84
    post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>;
85
    /**
86
     * Performs a request with `put` http method.
87
     */
88
    put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>;
89
    /**
90
     * Performs a request with `delete` http method.
91
     */
92
    delete(url: string, options?: RequestOptionsArgs): Observable<Response>;
93
    /**
94
     * Performs a request with `patch` http method.
95
     */
96
    patch(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>;
97
    /**
98
     * Performs a request with `head` http method.
99
     */
100
    head(url: string, options?: RequestOptionsArgs): Observable<Response>;
101
    /**
102
     * Performs a request with `options` http method.
103
     */
104
    options(url: string, options?: RequestOptionsArgs): Observable<Response>;
105
}
106
/**
107
 * @experimental
108
 */
109
export declare class Jsonp extends Http {
110
    constructor(backend: ConnectionBackend, defaultOptions: RequestOptions);
111
    /**
112
     * Performs any type of http request. First argument is required, and can either be a url or
113
     * a {@link Request} instance. If the first argument is a url, an optional {@link RequestOptions}
114
     * object can be provided as the 2nd argument. The options object will be merged with the values
115
     * of {@link BaseRequestOptions} before performing the request.
116
     *
117
     * @security Regular XHR is the safest alternative to JSONP for most applications, and is
118
     * supported by all current browsers. Because JSONP creates a `<script>` element with
119
     * contents retrieved from a remote source, attacker-controlled data introduced by an untrusted
120
     * source could expose your application to XSS risks. Data exposed by JSONP may also be
121
     * readable by malicious third-party websites. In addition, JSONP introduces potential risk for
122
     * future security issues (e.g. content sniffing).  For more detail, see the
123
     * [Security Guide](http://g.co/ng/security).
124
     */
125
    request(url: string | Request, options?: RequestOptionsArgs): Observable<Response>;
126
}
(6-6/14)