0

I have a Python filter to handle a few endpoints to do some configuration and calculation. For that I use the HTTP-GET method. Now, I want to use HTTP-POST to send some data to the Python filter.

Here is my example Request:

curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:8081/cgi-bin/qgis_mapserv.fcgi?SERVICE=POST_TEST

In the QGIS log file I can clearly see that the request is getting handled somehow:

[1303][12:28:26] REMOTE_ADDR: 172.23.0.1 [1303][12:28:26] CONTENT_TYPE: application/json [1303][12:28:26] HTTP_USER_AGENT: curl/7.47.0 [1303][12:28:26] QgsPostRequestHandler::parseInput [1303][12:28:26] QgsHttpRequestHandler::readPostBody [1303][12:28:26] length is: 35 [1303][12:28:26] {"username":"xyz","password":"xyz"} [1303][12:28:26] inserting pair SERVICE // POST_TEST into the parameter map [1303][12:28:26] Error at line 1, column 1: error occurred while parsing element.

But it is not possible to access the parsed information. I tried it with the same code-snipped which I use to retrieve information from a HTTP-GET Request:

request = self.serverInterface().requestHandler() params = request.parameterMap() service = params.get('SERVICE', '') body = request.body()

Well, I think that there are simple no bindings for Python to handle a HTTP-POST Request?

Version:

  • Apache/2.4.10
  • load library /usr/lib/qgispython (2.18.4)
  • Does the service GetCapabilities response say that it can handle HTTP POST? – nmtoken Mar 24 '17 at 18:46
  • Actually not, but i activated for example WFS in the *.qgs file. I can also do geatFeature-Requests. Maybe the problem lies in the log [1303][12:28:26] Error at line 1, column 1: error occurred while parsing element. – Sceada Mar 27 '17 at 7:29
  • But that would mean, that my curl request is not correct. – Sceada Mar 27 '17 at 7:34
  • Well for the WFS i can see the POST section. But i don't want to do a WMS or WFS request, it is completely independent from that. – Sceada Mar 27 '17 at 7:57
  • AFAIK, localhost:8081/cgi-bin/qgis_mapserv.fcgi? defines the service end-point to one or more OGC services, so you should only be able to use the service interfaces supported. – nmtoken Mar 27 '17 at 10:27
0

Ok, probably I found a workaround which is really bad. I found out, that the best way to get my information is to mix normal text with json. Let me show you what I mean.
curl -i -H "Content-Type: application/text" -X POST -d 'SERVICE=POST_TEST&json={"test":"test", "test2":[1,2,3,4], "test3":{"test4":"test5"}}' 'http://localhost:8081/cgi-bin/qgis_mapserv.fcgi?' -H "Accept: application/text"

What does QGIS do with the request body? It still produce one error message and then a parameter list.
Error at line 1, column 1: error occurred while parsing element. [832][08:53:52] inserting pair SERVICE // POST_TEST into the parameter map [832][08:53:52] inserting pair JSON // {"test":"test", "test2":[1,2,3,4], "test3":{"test4":"test5"}} into the parameter map

The parameter list is now accessible by the parameterMap.
request = self.serverInterface().requestHandler() params = request.parameterMap()

By the way
request = self.serverInterface().requestHandler() body = request.body() is always empty....

Ok back to the workaround. Now I can check first for the SERVICE parameter to pull the rest of the params to the right function and than I can parse the value for the json parameter.
requestBody = json.loads(params.get('JSON', '')) {u'test': u'test', u'test3': {u'test4': u'test5'}, u'test2': [1, 2, 3, 4]}

Well, as I said in the beginning I don't like this kind of workaround!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.