0

I am executing a web service using httpClient, but my problem is that the response headers are always empty, I already check this and this and can't make it work, this is my service:

iniciarSesion(usuario: Usuario) {

const idSistema = environment.idSistema;
const url = environment.url + environment.mssesiones;
return this.httpClient.post(
  url,
  {
    usuario: usuario.usuario,
    contrasena: usuario.contrasena,
    idSistema: idSistema
  },
  {
    headers: new HttpHeaders({
      'Content-Type':  'application/json'
    }),
    observe : 'response'
  });
}

And this is the component that execute the service:

this.inicioSesionService.iniciarSesion(usuario).subscribe(
        (httpResponse: HttpResponse<Object>) => {
          console.log(httpResponse);
      });

Can someone help me with this issue?,

Thanks in advance.

1 Answer 1

1

After searching I found that the problem was the service(spring boot service), I needed to add the Access-Control-Expose-Headers like this:

headers.add("authorization", jwtUtil.generateToken(usuario, peticion.getRemoteAddr()));
headers.add("Access-Control-Expose-Headers", "authorization");

I notice that the httpResponse was returning empty headers until I execute:

httpResponse.headers.get('authorization');

After that execution the console.log display the header information, I don't understand the behavior of that but now is working.

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

1 Comment

I also faced similar issue. I've set headers from server side to allow & expose all required headers. But still I keep getting empty headers in client side (Angular 6). But when I execute response.headers.get('Content-Type'); suddenly, I get all headers!!!

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.