9

In Angular 2 RC 4 I have a class HttpLoading which extends the original Http service of Angular 2.

With RC 4 I was able to use that in bootstrap without any issue using the below code:

bootstrap(AppComponent, [
    HTTP_PROVIDERS,
    provide(RequestOptions, { useClass: DefaultRequestOptions }),
    provide(Http, {
        useFactory: (backend: XHRBackend, defaultOptions: RequestOptions) => new HttpLoading(backend, defaultOptions),
        deps: [XHRBackend, RequestOptions]
    })
]).catch(err => console.error(err));

My DefaultRequest Options class

import { Injectable } from '@angular/core';
import { Headers, BaseRequestOptions } from '@angular/http';

@Injectable()
export class DefaultRequestOptions extends BaseRequestOptions {
    headers: Headers = new Headers({
        'Content-Type': 'application/json'
    });
}

My HttpLoading Class looks like this:

import { Http, RequestOptionsArgs, ConnectionBackend, RequestOptions, Response } from '@angular/http'
import { Injectable } from '@angular/core'
import {GlobalService} from './globalService';
import { Observable } from 'rxjs/Observable';

export class HttpLoading extends Http {

    constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private _globalService: GlobalService) {
        super(backend, defaultOptions);
    }

    get(url: string, options?: RequestOptionsArgs): Observable<any> {
        this._globalService.isLoading = true;
        return super.get(url, options)
            .map(res => {
                this._globalService.isLoading = false;
                return res.json();
            })
            .catch(this.handleError);
    }
}

With Angular 2 RC 5 the way bootstrap has been changed and we now need to have NgModule and bootstrap that. I have followed the migration guide and created my below NgModule:

import { NgModule }       from '@angular/core';
import { BrowserModule  } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule }   from '@angular/forms';
import { HttpModule }     from '@angular/http';
import { Http, HTTP_PROVIDERS, RequestOptions, XHRBackend } from '@angular/http';

import { DefaultRequestOptions } from './DefaultRequestOptions';
import { HttpLoading } from './http-loading';

import { routing }        from './app.routing';
import { AppComponent } from './components/app.component';


@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        ReactiveFormsModule,
        HttpModule,
        routing
    ],
    declarations: [
        AppComponent
    ],
    providers: [
        HTTP_PROVIDERS,
        { provide: RequestOptions, useClass: DefaultRequestOptions },
        {
            provide: Http,
            useFactory: (backend: XHRBackend, defaultOptions: RequestOptions, globalService: GlobalService) => new HttpLoading(backend, defaultOptions, globalService),
            deps: [XHRBackend, RequestOptions, GlobalService]
        },
GlobalService
    ],
    bootstrap: [AppComponent],
})
export class AppModule { }

But now when I use it in my Component, it doesn't sent any request to server and through below error in console:

EXCEPTION: Error: Uncaught (in promise): TypeError: Cannot read property 'toString' of null

My component in which I am using http is below:

import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';

@Component({
    templateUrl: '../../templates/home/home.html'
})
export class HomeComponent implements OnInit {

    constructor(private http: Http) {}

    ngOnInit() {
        this.http.get('/home')
            .subscribe((data) => {

            });
    }
}

Can anyone please guide?

3 Answers 3

20

I found the solution here. I had the same problem it worked for me.

Just add a body in in the get options:

this.http.get(url, { body: "" });

I hope it will work for you too

Sign up to request clarification or add additional context in comments.

3 Comments

For me too. I had the issue on a DELETE request. Thanks!
Yep, amazingly this worked for me as well. Is this intended behavior? Looks like a bug to me.
Looks like a bug that will be patched in rc6
2

Perhaps it's a typo but I can't see the GlobalService class in the provider of your NgModule:

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        ReactiveFormsModule,
        HttpModule,
        routing
    ],
    declarations: [
        AppComponent
    ],
    providers: [
        HTTP_PROVIDERS,
        GlobalService, // <----------
        { provide: RequestOptions, useClass: DefaultRequestOptions },
        {
            provide: Http,
            useFactory: (backend: XHRBackend, defaultOptions: RequestOptions, globalService: GlobalService) => new HttpLoading(backend, defaultOptions, globalService),
            deps: [XHRBackend, RequestOptions, GlobalService]
        }
    ],
    bootstrap: [AppComponent],
})
export class AppModule { }

1 Comment

Thierry, sorry that was just typo in the question. In my code, I have this in providers list but its still throwing error. "Error: Uncaught (in promise): TypeError: Cannot read property 'toString' of null"
1

Thank you so much everyone for your help, I was able to resolve the issue by removing

{ provide: RequestOptions, useClass: DefaultRequestOptions },

Which seems to be unnecassary with RC 5.

1 Comment

Do we need HTTP_PROVIDERS ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.