Which module I need to import to use HTTP from @ionic-native/http/ngx?

I trying to use: HTTP https://beta.ionicframework.com/docs/native/http

In the service, I import and use HTTP, but when I run, it responses NullInjectorError: No provider for HTTP!
I see that I missed the import into de Module app.module.ts but I don’t know which module I need to import. I try import import { HttpModule } from '@angular/http'; but doesn’t work…

PD. also, should not be specified inside this documentation?

  • Service:
// app/auth.service.ts
import { HTTP } from '@ionic-native/http/ngx';

...

export class AuthService {
  
  constructor( private http: HTTP ) { }

  public login(credentials) {
       ...

        this.http.get(url, {}, {})
        .then(data => {
          console.log('data:');
          console.log(data.status);
          console.log(data.data); // data received by server
          console.log(data.headers);

        })
        .catch(error => {
          console.log('error');
          console.log(error.status);
          console.log(error.error); // error message as string
          console.log(error.headers);
          Observable.throw("Error http:" + error.error);
        });

   ...

  • Modules
// app/app.module.ts
import { HttpModule } from '@angular/http';

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [
    BrowserModule,
    IonicModule.forRoot(),
    AppRoutingModule,
    HttpModule
  ],
...

bonjour,
regardez par ici https://github.com/ionic-team/ionic/issues/15199

I think the issue is , You are importing HttpModule after AppRoutingModule, place HttpModule after BrowserModule. Like this

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [
    BrowserModule,
    HttpModule,
    IonicModule.forRoot(),
    AppRoutingModule,
  ],

Order of Module Import matters , when they are dependent on each other. :+1:
Hope it will resolve the problem.

I modify the order and the error persist… (thanks anyway)

(console.error)

ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[AuthService -> HTTP]: 
  StaticInjectorError(Platform: core)[AuthService -> HTTP]: 
    NullInjectorError: No provider for HTTP!
Error: StaticInjectorError(AppModule)[AuthService -> HTTP]: 
  StaticInjectorError(Platform: core)[AuthService -> HTTP]: 
    NullInjectorError: No provider for HTTP!
    at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:1062)
1 Like

Finally, I used:

  • import { HttpClient, HttpHeaders } from '@angular/common/http'; into the Service.
  • And import import { HttpClientModule } from '@angular/common/http'; in app.module.ts

I still don’t understand how and why I can not use HTTP… but, I can continue.

{HTTP} from ‘@ionic-native/http/ngx’

HTTP is not a module it should be in provider

eg:

providers: [HTTP]
2 Likes

Thank you, this solved my issue. How were you able to tell that it was a provider and not a module?