0
return this.http.post(this.url + '/' + endpoint, body, {headers: {
      'customHeader1': 'changed',
      'customHeader2': 'changed2'       
    }});

The above snippet is my http request. I can make the call without the headers object, however the endpoint I'm calling won't return data until I set those headers. Adding those headers caused the app to return the below error:

    user.ts:50 ERROR TypeError: req.headers.forEach is not a function

How can I resolve this and set my headers in the http request?

These are my imports

import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Http, Response, RequestOptionsArgs, RequestMethod, Headers, Request 
} from '@angular/http';

This is my constructor

constructor(public http: HttpClient) {}

1 Answer 1

1

I usually create an options object with the 'headers'

let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers });

and then use it

this.http.post(this.url + '/' + endpoint, body, options)

as explained here: https://www.concretepage.com/angular-2/angular-2-http-post-example

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

1 Comment

Fixed the error I was getting, but the headers do not set in the request

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.