GeoServer REST API – Why ‘HTTP 406 Error’ When Adding New Rule to GeoServer GeoFence via RESTful API

geoservergeoserver-geofencegeoserver-rest-api

I'm trying to create a new rule in GeoServer GeoFence via GeoFence RESTful API, But I got HTTP 406 error, I don't know what thing I'm doing wrong, Because I did based on GeoServer GeoFence Plugin.

curl -v -u admin:g8****dg -X POST http://localhost:8080/geoserver/rest/geofence/rules -H "content-type:application/xml" -H "accept:application/xml" -d "
<Rule>
      <userName>[email protected]</userName>
      <workspace>qwBCMzLk24L7nd1dWdizH4zl5nnckGYU</workspace>
      <layer>*</layer>
      <service>*</service>
      <request>*</request>
      <access>DENY</access> </Rule>"
curl -v -u admin:g8****dg -X POST http://localhost:8080/geoserver/rest/geofence/rules -H "content-type:application/json" -H "accept:application/json" -d '{"Rule":{"userName":"[email protected]","roleName":"DEMO","workspace":"qwBCMzLk24L7nd1dWdizH4zl5nnckGYU","layer":"*","service":"*","request":"*","access":"DENY"}}'

I read these links also, But I didn't understand clearly and I didn't find a neat instruction that described step by step How users can create new Rule by GeoServer RESTful API

GeoFence Tutorial in GeoServer Documnent and GeoFence github

I think the path mentioned that GeoServer GeoFence tutorial has a tiny mistake!, instead of http://localhost:8080/geoserver/rest/geofence/rules, there was http://localhost:8080/geoserver/geofence/rest/rules.

I did try both of them but I didn't get result.

What is the correct form of adding new rule to GeoFence (GeoServer version) via RESTful API?

I'm using GeoServer 2.20.2 and Tomcat 9.0.58, Java v8, Debian 11 x64,and GeoServer Geofence server plugin.

Best Answer

Based on @IanTurton solution, He mentioned in comments.If run that command without "accept:application/xml" user will be able to add new rule.

The only header is need is "content-type:application/xml" or if you are trying to send json format use "content-type:application/json".

curl -v -u admin:g8****dg -X POST http://localhost:8080/geoserver/rest/geofence/rules -H "content-type:application/xml" 
-d "
<Rule>
     <userName>[email protected]</userName>
  <workspace>
      qwBCMzLk24L7nd1dWdizH4zl5nnckGYU
  </workspace>
      <layer>*</layer>
      <service>*</service>
      <request>*</request>
      <access>DENY</access>
</Rule>"

Related Question