Reputation: 9
I have upgraded the code from Angular 2 to Angular 6. The parameters set in the http headers are the content-type
and authorization
. The authorization
contains btoa
of user name and password.
I could not find what is the error in setting up the parameters for httpheaders.
This is for authorization from Spring boot. The code for appmodule
and the service
are attached.
app module
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule,HttpHeaders } from '@angular/common/http';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MenuComponent } from './components/menu/menu.component';
import { LoginComponent } from './components/login/login.component';
import { AngularMaterialModule } from './angular-material.module';
import { AppRoutingModule } from './/app-routing.module';
import { FormsModule } from '@angular/forms';
@NgModule({
declarations: [
AppComponent,
MenuComponent,
LoginComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
HttpClientModule,
AngularMaterialModule,
AppRoutingModule
],
providers: [HttpHeaders],
bootstrap: [AppComponent]
})
export class AppModule { }
Login Service
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders} from '@angular/common/http';
import { Token } from '../models/Token';
@Injectable({
providedIn: 'root'
})
export class LoginService {
constructor(private http: HttpClient,private header: HttpHeaders) { }
sendCredential(username: string, password: string) {
let url = 'http://localhost:8080/token';
let headers = new HttpHeaders({
'Content-Type' : 'application/x-www-form-urlencoded',
'Authorization' : 'Basic ' + btoa(username+":"+password)
});
return this.http.get<Token>(url, { headers });
}
checkSession() {
let url = 'http://localhost:8080/checkSession';
let headers = new HttpHeaders({
'x-auth-token' : localStorage.getItem('xAuthToken')
});
return this.http.get(url, { headers });
}
logout() {}
}
Upvotes: 0
Views: 277
Reputation: 9
Thanks for the responses.
It got resolved by specifying the statement provide: HttpHeaders in app module.
The statement is
providers: [{provide: HttpHeaders}]
Upvotes: 0