1

Looking at this older question, I'd like to know if it is possible to make a POST request to retrieve features from a GeoServer WFS starting from a JSON data object.

I have this data object which I'd like to transform in an XML GetFeature request suitable for GeoServer.

const featureRequest = {
  service: 'WFS',
  request: 'GetFeature',
  typename: foo:baz,
  outputFormat: 'application/json',
  srsname: 'EPSG:3857',
  maxFeatures: 50,
  CQL_FILTER: ''
};

And I'd like to use it to send request via POST method using Angular http client like so:

const baseUrl = environment.geoserverBaseUrl;
const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'application/xml'
  })
};
const response$ = this.http.post<FeatureCollection>(baseUrl, JSON.stringify(featureRequest), httpOptions)
  .pipe(catchError(this.handleError));
return response$;

The code above fails and the web console reads Backend returned code 200, body was: [object Object].

GeoServer logs:

org.xmlpull.v1.XmlPullParserException: only whitespace content allowed before start tag and not { (position: START_DOCUMENT seen {... @1:1) 

I understand the problem should be that I need to format the request to a sort of XML OGC GetFeature request like (am I correct?). However, is there an easy way to do that (like with a file storing the structure where I can substitute the parameters I need to request?

I am using OpenLayers and I also tried the writeGetFeature() method using the filter option, but I found the filter to be too tricky to build when more than (saying) three parameters are involved in the query, and I'd like to go back to the good old plain CQL_FILTER.

3
  • 1
    Yes, you need to conform to the WFS standard for getFeature requests.
    – Ian Turton
    Sep 22 '20 at 8:29
  • Hi Ian. Is there an easy way to do that without (saying) going through all the keys and values of my data object and put them in a mega string XML like conforming to the WFS standard? I wish there was a way (a library?) that could allow me to do that using a XML file which has the skeleton of a correct GetFeature request which I can use combined with my object. Do you know if anything like that exist? Or would it be easier to just compose the string?
    – umbe1987
    Sep 22 '20 at 8:34
  • 1
    mmmm I just came across this question, which made me think that CQL_FILTER will not be allowed in standardized OGC WFS GetFeature requests, so I think I need to stick with the filter option of OL writeGetFeature(), OR I should switch to GET requests... If the URL does not get too long I might think to go with the latter probably...
    – umbe1987
    Sep 22 '20 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.