What does the Angular "strict-origin-when-cross-origin" Error mean?

John F. picture John F. · May 17, 2021 · Viewed 8.4k times · Source

From my app I want to reach an api. The curl request is:

curl --request POST   https://...    --header 'Authorization: Token ...'   --header 'Accept: application/csv'   --header 'Content-type: application/vnd.flux'   --data '...'

It works and returns data from an InfluxDB instance. The problem ist when I try everything in Angular: I set up an proxy which redirects the requests correctly(I check this in the terminal). But in the web browser (Firefox Developer Edition) I get

POSThttp://localhost:4200/api/
[HTTP/1.1 401 Unauthorized 204ms].

Status 401
Unauthorized
Version HTTP/1.1
Übertragen 350 B (55 B Größe)
Referrer Policy strict-origin-when-cross-origin

The api URL is a https address, same error happens with http. Same happens in the production app.

proxy.conf

{
"/api/*": {
"target": "...",
"secure": false,
"logLevel": "debug",
"changeOrigin": true
}
}

Terminal confirmation:

[HPM] POST /api/ -> correct url

Request in component:

apiurl = '/api/';
data = JSON.stringify('request');
headers:HttpHeaders = new HttpHeaders();;

constructor(private route: ActivatedRoute,
private httpClient: HttpClient) {
this.headers.set('Authorization', 'Token token');
this.headers.set('Content-type', 'application/vnd.flux');
}

getConfig() {
return this.httpClient.request<any>('post',`${this.apiurl}`,
    {
      body: this.data,
      headers: this.headers
    }
 );
}


ngOnInit(): void {

this.getConfig()
.pipe(first())
.subscribe(
 data => {
   console.log(data);
 },
 error => {
   console.log(error);
 });
  }

I would appreciate a lou your ideas, thank you!

Answer

Lenzman picture Lenzman · May 18, 2021

Change your proxy configuration.

From this:

{
"/api/*": {
"target": "...",
"secure": false,
"logLevel": "debug",
"changeOrigin": true
}
}

To this:

{
"/api/*": {
"target": "...",
"secure": false,
"logLevel": "debug",
"changeOrigin": true,
"pathRewrite": { "^/api": "" }
}
}