Options
All
  • Public
  • Public/Protected
  • All
Menu

Class RequestBuilder

Chainable HTTP request builder.

example

let res: Observable<Response> = new RequestBuilder(http)
  .post('http://example.com')   // Set the URL
  .body({ foo: 'bar' })         // POST a JSON body with Content-Type application/json
  .execute();                   // Execute the HTTP request

let res2: Observable<Response> = new RequestBuilder(http)
  .get('http://example.com')
  .header('Authorization', 'Bearer secret')   // Set headers
  .search('page', 2)                          // Set search params
  .search('pageSize', 20)
  .execute();

Hierarchy

  • RequestBuilder

Index

Constructors

constructor

Methods

appendAllSearchParams

  • appendAllSearchParams(params: URLSearchParams): this

appendHeader

  • Appends a value to the existing list of values for a header.

    example
    
    builder.header('Link', '<http://example.com/list?page=3&per_page=100>; rel="next"');
    builder.header('Link', '<http://example.com/list?page=50&per_page=100>; rel="last"');
    

    Parameters

    • name: string
    • value: string

    Returns RequestBuilder

appendSearchParam

  • appendSearchParam(param: string, value: string): this

body

  • Sets the request's body (and optionally its content type).

    By default, the content type is application/json and the specified content is serialized with JSON.stringify.

    example
    
    builder.body({ foo: 'bar' });
    

    Parameters

    • content: any
    • Optional contentType: string

    Returns RequestBuilder

deleteHeader

deleteSearchParam

  • deleteSearchParam(param: string): this

execute

  • execute(http?: Http): Observable<Response>
  • Executes the configured HTTP request.

    example
    
    builder.execute().subscribe((res) => {
      // handle the response
    });
    

    Parameters

    • Optional http: Http

      The Http service with which to execute the request (defaults to the one supplied to the constructor).

    Returns Observable<Response>

header

interceptor

method

  • Sets the request's HTTP method (e.g. GET, POST).

    example
    
    builder.method('HEAD');
    builder.method(RequestMethod.Patch);
    

    Parameters

    • method: string | RequestMethod

    Returns RequestBuilder

modify

  • modify(func: function): this
  • Supplies this builder's internal RequestOptions object to the specified function for modification.

    This is provided so that request options can be freely modified if the provided chainable methods are not sufficient.

    example
    
    builder.modify((requestOptions: RequestOptions) => {
      requestOptions.method = RequestMethod.Post;
      requestOptions.body = JSON.stringify({ foo: 'bar' });
      requestOptions.headers = new Headers({
        'Content-Type': 'application/json'
      });
    });
    

    Parameters

    • func: function
        • (options: RequestOptions): any
        • Parameters

          • options: RequestOptions

          Returns any

    Returns this

modifyHeaders

  • modifyHeaders(func: function): this
  • Supplies this builder's internal Headers object to the specified function for modification.

    This is provided so that headers can be freely modified if the provided chainable methods are not sufficient.

    example
    
    builder.modifyHeaders((headers: Headers) => {
      if (!headers.has('Authorization')) {
        headers.set('Authorization', 'Bearer secret');
      }
    });
    

    Parameters

    • func: function
        • (headers: Headers): any
        • Parameters

          • headers: Headers

          Returns any

    Returns this

replaceAllSearchParams

  • replaceAllSearchParams(params: URLSearchParams): this

search

  • search(param: string, value: string): this

setAllSearchParams

  • setAllSearchParams(params: URLSearchParams): this

setHeader

  • Sets or overrides the header value for the specified name.

    example
    
    builder.setHeader('Authorization', 'Bearer secret');
    

    Parameters

    • name: string
    • value: string

    Returns RequestBuilder

setOptions

setResponseType

  • setResponseType(responseType: ResponseContentType): this

setSearchParam

  • setSearchParam(param: string, value: string): this

setWithCredentials

  • setWithCredentials(withCredentials: boolean): this

url

Generated using TypeDoc