Posting JSON from Swift to Zope

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?

1 Like

To send a POST request in Python (as Zope is) I would utilize the Requests: HTTP for Humans™ — Requests 2.27.1 documentation library.

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.

1 Like

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:

  1. Create a python script in Zope
  2. Set the url to post to the python script directly (here's an example of what the url would look like: www.foo.com/pythonscript OR www.foo.com/pythonscript?var=X, I don't know)
  3. The python script will know how to handle the incoming json because of X done in the python script
  4. Here's a small example of a python script that accepts the http post of json

Zope does not know anything specific about JSON. JSON is just plain text for Zope.

1 Like

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:

  1. Request comes from the app and posts to the zope url (a python script)
  2. 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).

1 Like

AHHH! Thank you. Post data is available as request["BODY"].

That's exactly what I needed to know. Now I can simply take request["BODY"] and use python's json to decode it and send it through to the zsql method.

Really appreciate that Dieter.

1 Like

+1 for requests