0

please forgive me if my question seems to have and answer. These don't works for me.

Basically I new in angular 6. I use http post to authenticate user. My code looks like this.

onSubmit(value){
    const httpOptions = {
        headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
        observe: 'response' as 'body'
    };

    this.http.post(Globals.authEndPoint, value, {observe: 'response'})
        .subscribe(function(res){
            console.log(res);
        });
}

On the server side:

app.use(function(req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "*");

        if (req.method === 'OPTIONS'){
            res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET' );
            return res.status(200).json({})
        }
        next();
    });

and this:

 res.status(200).header( 'X-Auth-Token', 'token-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x').send(user);

my request in angular is done like this:

I have found a question like my mine, but the answer is not working for my case.

I think I am doing something wrong. How can I get token directly in post response.

Any ideas ?

enter image description here

enter image description here

update:

I have tried the answer listed below.

getConfigResponse(value): Observable<HttpResponse<Config>> {
    return this.http.post<Config>( Globals.authEndPoint, value, {observe: 'response'});
}

this time I get one header. enter image description here

dmx
  • 1,862
  • 3
  • 26
  • 48
  • This is a bit confusing - what are you expecting to happen, and what is happening that is unexpected? And why are you putting an authentication token on the response to an OPTIONS request? – Daniel Schaffer Nov 15 '18 at 23:46
  • @DanielSchaffer I was expecting to have authentication token in my response(in angular 6). Basically I want to get headers that are shown on chrome dev tool directly in my angular response. – dmx Nov 15 '18 at 23:52
  • @dmx if it was you who upvoted my answer, then please mark it as the correct answer so that other people can benefit from it in the future. – ams Nov 16 '18 at 00:42

1 Answers1

1

Have you tried reading the full response? Also I would suggest that you use the HttpClient if you're using Angular 4.3.x and above, if you're not already doing it.

HttpClient reading the full response

showConfigResponse() {
  this.configService.getConfigResponse()
    // resp is of type `HttpResponse<Config>`
    .subscribe(resp => {
      // display its headers
      const keys = resp.headers.keys();
      this.headers = keys.map(key =>
        `${key}: ${resp.headers.get(key)}`);

      // access the body directly, which is typed as `Config`.
      this.config = { ... resp.body };
    });
}
ams
  • 449
  • 5
  • 10
  • thanks for your answer. I haved tried, you answer, but I am getting only `Content-Type` as header. – dmx Nov 16 '18 at 05:47
  • @dmx have you tried out exposing the `Access-Control-Expose-Headers` in your backend? See my answer [Here](https://stackoverflow.com/questions/53309992/problem-with-retrieving-headers-in-angular-6/53310116#53310116) for instructions. – ams Nov 16 '18 at 09:50
  • if you look closely on server side code provided, you can see that this has been done. – dmx Nov 16 '18 at 13:59
  • @dmx Maybe I'm blind, but I only see `Access-Control-Allow-Origin`, `Access-Control-Allow-Headers` and `Access-Control-Allow-Methods`, but not the `Access-Control-Expose-Headers` I mentioned? If you want clients to be able to access other headers, you have to use the Access-Control-Expose-Headers header. – ams Nov 16 '18 at 14:33