I have an issue to get the http response from my server. I want to reproduce a authentication login with HttpClient into my login.service.ts file.
I tried 2 last hours to find a solution but nothing is working...
Here is the code i use for it :
login.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class LoginService {
constructor(private http: HttpClient) { }
login(){
this.http.post('http://localhost:3000/auth', null, {headers: {
'x-login': 'admin',
'x-password': '8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918'}})
.subscribe(myToken => console.log(myToken));
}
}
I used PostMan to test the login and password, /auth respond a token : 653db89e-b80e-47f8-834a-62275b04fcbb.
The Content-Type is not a JSON response, this is a text/html; charset=utf-8 and i think that is maybe the problem but I don't really know.
The error is :
HttpErrorResponse {headers: HttpHeaders, status: 201, statusText: "Created", url: "http://localhost:3000/auth", ok: false, …}
error: {error: SyntaxError: Unexpected number in JSON at position 1 at JSON.parse (<anonymous>) at XMLHttp…, text: "08951337-479a-4e53-8b1b-823d29d89139"}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure during parsing for http://localhost:3000/auth"
name: "HttpErrorResponse"
ok: false
status: 201
statusText: "Created"
url: "http://localhost:3000/auth"
__proto__: HttpResponseBase
Any help is welcome !
I try to learn Angular 7, if what I say is not clear, do not hesitate to ask me for more informations.
Thanks !
I added the reponseType: "text"
parameter to my post() and it worked !
That is the full operation :
this.http.post('http://localhost:3000/auth', null, {headers: {
'x-login': 'admin',
'x-password': '8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918'},
responseType: "text" })
.subscribe(myToken => console.log(myToken)); // Output : 43307067-0c23-47ae-8085-402959e1f157