Do I need a webscript to call external rest server (CORS)?

cancel
Showing results for 
Search instead for 
Did you mean: 
kaffi
Partner

Do I need a webscript to call external rest server (CORS)?

I try to access another Rest service outside from Alfresco via ADF. Everytime I try to execute the call. I get the following error:

Access to XMLHttpRequest at 'https:/host/endpoint' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I also added the endpoint to the proxy.conf.json

"/endpoint": {
"target": "https://hostname",
"secure": false,
"changeOrigin": true
}


With the same result as before.

I use the following class:

export class ClassService {

constructor(private http: HttpClient) {
}

private getLoginHeader(username: string, password: string) {

let Headers: HttpHeaders = new HttpHeaders();
console.log('Header ' + 'Basic ' + btoa(username + ':' + password));
Headers = Headers.append('Authorization', 'Basic ' + btoa(username + ':' + password));
Headers = Headers.append('Access-Control-Allow-Origin', '*');
Headers = Headers.append('Content-Type', 'application/x-www-form-urlencoded');
console.log(Headers);
return Headers;
}

getBatchClasses(username: string, password: string ) {

return this.http.get<any>('/dendpoint',
{headers: this.getLoginHeader(username, password)}).subscribe(response => {
console.log(response);
}, err => {
console.log('User authentication failed!');
});
}


logout() {


localStorage.removeItem('token');
}

}

If I try to access it with Postman it works properly. That sounds for me, that ADF forces to use a repository webscript to access external resources. Is this correct? If not can somebody give me a hint?

Regards

Kaffi

5 Replies
yogeshpj
Established Member

Re: Do I need a webscript to call external rest server (CORS)?

If you have made a change on proxy.conf.json then make sure that you are not touching app.config.json file.

OR  else enable CORS in alfresco.

Please follow the link for ref https://community.alfresco.com/community/application-development-framework/blog/2017/06/20/adf-cors-... 

kaffi
Partner

Re: Do I need a webscript to call external rest server (CORS)?

Thanks for the answer, but my question was more about the external rest service I want to call which is not running in Alfresco. Is it possible to access this external rest service directly, because I always get the CORS error, or do I need to access it over a webscript?

sanjaybandhniya
Intermediate

Re: Do I need a webscript to call external rest server (CORS)?

In that external rest service ,you need to enable CORS.

mdtabrezmca
Established Member II

Re: Do I need a webscript to call external rest server (CORS)?

If you are running ADF application on nginx then you can use proxy and then allow CORS on your system.

d_moeyersons
Customer

Re: Do I need a webscript to call external rest server (CORS)?

This actually isn't an ADF specific question. It doesn't depend on ADF if you need CORS to access an external REST service or not.


Do you need to configure CORS? It depends.
Does the REST service run on the same domain & port or on another domain or port?

In the first case you don't need it, in the second case you do.
Also read this: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

If it is an external resource (like in not a company resource), why isn't CORS already enabled on the resource? CORS needs to be configured on the external service, not on in ADF/Angular. Adding a CORS header client side won't help you.
You always could proxy that resource (that's basically what proxy.conf.js does), but if CORS isn't present, are you allowed to consume that resource in that way?