1

I instantiate and register WebManager as part of my app. I'm running a workflow and web app together. Everything works find, except for getting POST data.

class WebSite(Controller):
    def index(self):
        return "Hello World!"

    def detail(self, id):
        return "Web ID: {}".format(id)


class WebService(Controller):

    channel = "/WebService"

    def POST(self, *args, **kwargs):
        return str(args) + ' ' + str(kwargs) + ' ' + 'Why are these empty?'


class WebManager(Component):
    def init(self):
        Server(('0.0.0.0', 80)).register(self)
        WebSite(self).register(self)
        WebService(self).register(self)

Using the following to call:

import requests

r = requests.get('http://localhost')
print(r.text)
# Output: Hello World!

r = requests.get('http://localhost/detail?id=12')
print(r.text)
# Output: Web ID: 12

r = requests.post('http://localhost/WebService', json={'bob': 1, 'joe': {'blue': 43}})
print(r.text)
# Output: () {} Why are these empty?

I been through the docs and can't figure out how I get the body data of the post. I assumed it would be passed in as one of the arguments. I've also stopped in PyCharm debugger and looked through the self object of WebService and see nothing.

Joe
  • 2,407
  • 18
  • 20

1 Answers1

1

Why is it always that you find the answer just after you ask the question?

When the POST method executes, there is a .request.body property of the Controller. This is an io.BytesIO object, so read like a file and gets bytes sent in.

class WebService(Controller):

    channel = "/WebService"

    def POST(self):
        # I'm using JSON, so decoding the bytes to UTF-8 string to load
        data = json.loads(self.request.body.read().decode('UTF-8'))
        return 'Data: {}'.format(data)
Joe
  • 2,407
  • 18
  • 20