0
votes

I need to send from my server side to API in request headers "Cookie: token" In angular universal, for servers Http request methods I use axios. when I try to change headers using Interceptor I have an error "Refused to set unsafe header 'Cookie'" if I send a static cookie like third arguments of axios post all work fine but I have some troubles to dynamically insert token there.

request.servise.ts

    import { RequestBody } from './models/request-body.model';
    import axios from 'axios';
    import {AxiosInstance} from 'axios';

    export class Request {
        http: AxiosInstance = axios;
        constructor() {}
        async test(): Promise<any> {
        const  params = {
                param1: 1,
                param2: 2,
                param3: 3,
            }
            try {
            return this.basicRequest('https://some-request.url', params);
            } catch (e) {
            console.error('Unknown exception: ', e);
            return null;
            }
        }

        private basicRequest(url, params) {
            const request = new RequestBody('2.0', 'someMethod', Math.floor(Math.random() * (9999999 - 1000000)) + 1000000, params);
            return this.http.post(url, request);
        }
    }

projects.ts (sever router controller)

      import {Router} from 'express';
      import {Request} from '../../shared/request.service';



      const router: Router = Router();

      router.get('/test', async function(req, res, next){

        const request = new Request();
        try {
          const projects = (await request.test()).data.result.records;
          res.json(projects);
        } catch (e) {
          console.error(e);
        }
      });


      export const ProjectsController: Router = router;
1

1 Answers

0
votes

By default, angular universal does not transfert cookieswhen using HttpClient. So, you need to do it manually, but you'll get the error you mentionned.

A possible workaround, suggested in that universal github issue, is to bypass xhr2's default security behaviour for unsafe headers

server.ts

import * as xhr2 from 'xhr2';
xhr2.prototype._restrictedHeaders = {};