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

31.9k Views Asked by At

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 is 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 look your ideas!

1

There are 1 best solutions below

0
On BEST ANSWER

You missed "pathRewrite": { "^/api": "" }

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": "" }
 }
}