Request

Creates Request instances from provided values.

查看"说明"...

已弃用: see https://angular.io/guide/http

      
      class Request extends Body {
  constructor(requestOptions: RequestArgs)
  method: RequestMethod
  headers: Headers
  url: string
  withCredentials: boolean
  responseType: ResponseContentType
  detectContentType(): ContentType
  detectContentTypeFromBody(): ContentType
  getBody(): any
}
    

说明

The Request's interface is inspired by the Request constructor defined in the Fetch Spec, but is considered a static value whose body can be accessed many times. There are other differences in the implementation, but this is the most significant.

Request instances are typically created by higher-level classes, like Httpand Jsonp, but it may occasionally be useful to explicitly create Request instances. One such example is when creating services that wrap higher-level services, like Http, where it may be useful to generate a Request with arbitrary headers and search params.

import {Injectable, Injector} from '@angular/core'; import {HTTP_PROVIDERS, Http, Request, RequestMethod} from '@angular/http'; @Injectable() class AutoAuthenticator { constructor(public http:Http) {} request(url:string) { return this.http.request(new Request({ method: RequestMethod.Get, url: url, search: 'password=123' })); } } var injector = Injector.resolveAndCreate([HTTP_PROVIDERS, AutoAuthenticator]); var authenticator = injector.get(AutoAuthenticator); authenticator.request('people.json').subscribe(res => { //URL should have included '?password=123' console.log('people', res.json()); });
      
      
  1. import {Injectable, Injector} from '@angular/core';
  2. import {HTTP_PROVIDERS, Http, Request, RequestMethod} from '@angular/http';
  3.  
  4. @Injectable()
  5. class AutoAuthenticator {
  6. constructor(public http:Http) {}
  7. request(url:string) {
  8. return this.http.request(new Request({
  9. method: RequestMethod.Get,
  10. url: url,
  11. search: 'password=123'
  12. }));
  13. }
  14. }
  15.  
  16. var injector = Injector.resolveAndCreate([HTTP_PROVIDERS, AutoAuthenticator]);
  17. var authenticator = injector.get(AutoAuthenticator);
  18. authenticator.request('people.json').subscribe(res => {
  19. //URL should have included '?password=123'
  20. console.log('people', res.json());
  21. });

构造函数

constructor(requestOptions: RequestArgs)
      
      constructor(requestOptions: RequestArgs)
    
参数
requestOptions RequestArgs

属性

属性说明
method: RequestMethod

Http method with which to perform the request.

headers: Headers Headers

instance

url: string

Url of the remote resource

withCredentials: boolean

Enable use credentials

responseType: ResponseContentType

Buffer to store the response

方法

Returns the content type enum based on header options.

detectContentType(): ContentType
      
      detectContentType(): ContentType
    
参数

没有参数。

返回值

ContentType

Returns the content type of request's body based on its type.

detectContentTypeFromBody(): ContentType
      
      detectContentTypeFromBody(): ContentType
    
参数

没有参数。

返回值

ContentType

Returns the request's body according to its type. If body is undefined, return null.

getBody(): any
      
      getBody(): any
    
参数

没有参数。

返回值

any