Http native header not sending

I am developing an app with HTTP. I want to send the data with headers. Data is successfully sending without header.
I am used the steps in

the code is

let header = new HttpHeaders();
header = header.set('a', 'abc');

    this.http.post(this.apiUrl+'/login', {postParams}, {header}).then(data => {
        console.log(data.status);
        console.log(data.data); // data received by server
        console.log(data.headers);
    var json = JSON.stringify(data.headers);
    console.log(JSON.stringify(data.headers["x-auth-token"]));

})
.catch(error => {

    console.log(error.status); // showing as "undefined"
    console.log(error.error); // showing as "undefined"
    console.log(error.headers); // showing as "undefined"
});

The import files are

import { Injectable } from '@angular/core';
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { Headers, RequestOptions  } from '@angular/http';
import { HTTP } from '@ionic-native/http';

I have tried different methods but not getting the result.

If adding

this.http.post(this.apiUrl+'/login', {postParams}, {})

Geeting the result but when adding

this.http.post(this.apiUrl+'/login', {postParams}, {header})

Errors showing “undefined” .

Please help me.

Please check with your backend team that whether they are accepting headers in URL

I check the backend not accepting when adding header in request. But in postman it is working.

I got the solution when i add header directly in the request.

this.http.post(this.apiUrl+'/login', {postParams}, {"Content-Type": "application/json","auth":"anbnvbnhvbnbnvbnvbnbnbn"})

This is working for me.

1 Like