angularangular-httpclient

Angular HTTPClient header returns length of undefined error


Trying to make a simple POST call but getting the error:

TypeError: Cannot read properties of undefined (reading 'length')
    at HttpHeaders.applyUpdate (http.mjs:222:27)
    at http.mjs:195:58
    at Array.forEach (<anonymous>)
    at HttpHeaders.init (http.mjs:195:33)
    at HttpHeaders.forEach (http.mjs:263:14)
    at Observable._subscribe (http.mjs:2116:29)
    at Observable._trySubscribe (Observable.js:37:25)
    at Observable.js:31:30
    at errorContext (errorContext.js:19:9)
    at Observable.subscribe (Observable.js:22:21)

I'm setting the base headers and making the post request:

let headers = new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded'});
    this.http.post(
      '<target domain>',
      {
        grant_type: '<grant_type>',
        code: '<code>',
        redirect_uri: '<redirect_uri>',
        client_id: '<client_id>'
      },
      {
        headers
      }
    ).subscribe({
      next: (response) => {
        console.log('Token response:', response);
      },
      error: (error) => {
        console.log('error:', error);
      }
    });

The headers variable is just the default new HttpHeaders values with Content-Type added. It seem like it should work as expected. Am I missing something?


Solution

  • The issue is not from the code shared, usually the API calls, will pass through an interceptor, so identify the problematic interceptor that might cause the error.

    Reference Stackblitz