Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (19.9k points)

I am trying to set header from http post call, But When I am consoling it to the log, It is printing undefined.

I tried the same for params also, but it is not getting set.

callAddGroupAPI(formId, data){

    const headers = new Headers()

    .set('Content-Type', 'application/json');

    console.log(headers);

}

I am expecting output in the console to log the value of headers, But it is print undefined.

I have also tried

callAddGroupAPI(formId, data){

    const headers = new Headers();

    headers.append('Content-Type','application/json');

    console.log(headers);

}

And this also is not working...

I am getting the same problem when I am using

const data = new FormData();

data.set('key', 'value')

console.log(data)

I have use .append() also, But that also is not working.

1 Answer

0 votes
by (25.1k points)
edited by

To use headers try this,

/* define in your service.ts constructor */

this.httpOptions = {

  headers: new HttpHeaders({ 'Content-Type': 'application/json', 'X-CSRFToken': csrf }),

};

/* Make http call after agging above code like this: */

this.http.post(yoururl, payload, this.httpOptions);

About formdata, when you do console.log() it'll just printFormData {}, to access the key do data.getAll('key') or use data.forEach

Are you willing to pursue a career in Angular, here's an opportunity for you; Angular Training provided by intellipaat!

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...