I work on Angular and I have to use the Geoserver API to publish data located in a spatial database. I tried with PHP Curl and the API worked pretty well. Now I want to use it in my Angular app.
I must precise that Geoserver (version 2.22) is installed on Apache Tomcat (9.0) and all the tools are located in the same server.
Here is my code :
HTML :
<button type="button" (click)="testFct()">OK</button>
TS component file :
testFct(){
var layer = 'region'
this._apiService.PostData(layer)
.subscribe(data => {
console.log(data)
})
}
Provider file (api.service.ts) :
PostData(layer:any) {
let authorizationData = 'Basic ' + btoa('user' + ':' + 'password');
const body = '<featureType><name>' + layer + '</name></featureType>';
var options = new HttpHeaders()
.set('Access-Control-Allow-Origin', '*')
.set('Content-Type', 'text/xml')
.set('Authorization', authorizationData);
return this.http.post('http://IP:8080/geoserver/rest/workspaces/MyWorkspace/datastores/MyDatastore/featuretypes', body, { headers: options })
}
When I click on the "OK" button I have CORS Missing Allow Origin problems :
Access to XMLHttpRequest at 'http://IP:8080/geoserver/rest/MyWorkspace/pampas/MyDatastore/bdd/featuretypes' from origin 'http://IP:4201' 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.
In read number of CORS subjects on Stack Overflow or outside, tried many and many things but I'm still getting stuck and a completely lost.
I added these lines in /opt/tomcat/conf/web.xml, restarted Tomcat but nothing changes and I still have this Cors policy error :
Edit : add cors.allowed.headers params and set it to '*'
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Edit 2 : If I look at the network part of the browser it says :
POST : blocked : NS_ERROR_DOM_BAD_URI
OPTIONS : blocked : CORS Missing Allow Origin
I know there are many similarly questions on CORS errors but unless I am mistaken I did not find any subject dealing with the use of the Geoserver API in an Angular app .
Could you help me please ? Any help would be greatly appreciatd !