I already have python scripts in Zope doing zsql calls and encoding the results in json in order to work with my iOS app. However, I need to send data from the app into Zope, and then run a zsql method. That's where I'm a bit lost.
To encode the JSON I am using a python script, importing json, and telling it what to do. Then I call to that python script via a url call in my iOS app and decode it using swift. Super easy.
Now I need to go the other way and what I cannot understand is how to write a python script so that when I post my json from the app to the python script, the python script will use that posted json.
Working with remote data, you would normally add parameters to the python script and then pass those in via url variables (i.e. foo.com/pythonscript?email=blah@foo.com). You add email as a parameter to the python script and away you go. How do you do this when creating a post request with json?
To receive a POST with JSON in Zope there are plenty of options. But the rich request object in Zope helps here a lot. At least I would look at plone.rest (does not depend on Plone) and plone.restapi (very Plone) for inspiration.
If it comes to swift and sending JSON requests this is probably the wrong forum. I have no idea how, but you might be lucky and someone else knows.
Thanks. I don't need assistance with Swift. I am simply trying to understand specifically how to get zope to accept an http post request containing a json string. Sending the json and setting the http header, content-type, etc. is already in place.
For example, I am looking for the data flow explanation like this:
Hey Andreas. Not sure if you remember me or not, but many years ago we talked quit a bit in the #zope channel on IRC.
Thank you for confirming that. I suspected exactly the same. So, let me ask another way.
If I am posting a string through a URL, normally I would say foo.com/script?email=blah@foo.com and then specify email as a parameter to the python script. Since there is no parameter to specify in the case of sending a post request (i.e. just foo.com/script) how do I use the json string that has been posted in a python script?
For example, because there is no url variable, I can't specify a parameter in the python script to look for.
Here's what I actually need to do:
Request comes from the app and posts to the zope url (a python script)
Python script accepts that data (somehow, which is what I'm asking) and then I can decode it and use it to call a zsql method and return success or failure
Can you show me an example python script that would accept the json string? That's where I'm not understanding the data flow, or rather how to tell the python script to use that json string that is posted.
kittonian via Plone Community wrote at 2022-2-3 18:10 +0000:
...
Now I need to go the other way and what I cannot understand is how to write a python script so that when I post my json from the app to the python script, the python script will use that posted json.
There are several options:
for a POST request, the post body is available as request["BODY"];
once you have the JSON string, decoding is similar to encoding
you can install dm.zopepatches.ztutils.
It has a function register_json_extension.
Calling this function (during startup)
makes a ":json" type specifier available.
Request variables with that suffix are then
automatically json decoded.
Having called register_json_extension
(during startup) allows in addition that make_query and make_hidden_input (from ZTUtile)
can use json to encode parameter values.
This supports deeply nestes parameter structures.
I have seen a discussion about supporting :json in
Zope core. I had the impression that it would already be available
but apparently, it is not yet in Zope 4 (maybe only in Zope 5).