0

I am trying to implement a simple restful server using asyncio module in python 3.4 and not able to hit my endpoints I have defined. When I do a CURL (GET request) on the URL (http://127.0.0.1:8080/v1/queue, I get an empty response from the server. I think there's some issue with my basic class set up of asyncio. Could someone point that out? Does the CURL need to be implemented in a asyncio way where it uses aiohttp to send requests ?

Here is my code:

  4 import asyncio
  5 import aiorest
  6 import json
  8 
 10 
 11 class Sample(aiorest.RESTServer):
 12 
 13     def _init__(self, *args, **kwargs):
 14         super().__init__(*args, **kwargs)
 15         self.add_url('GET', '/v1/queue', self.get_handler)
 16         self.add_url('POST', '/v1/stack', self.post_handler)

 19     def get_handler(self, request):
 20         return {'Welcome': 'user'}
 21 
 22     def post_handler(self, request, payload):
 23         if not payload:
 24             return {'error': 'Payload not supplied'}
 25 
 26         try:
 27             json_payload = json.loads(payload)
 28         except ValueError:
 29             return {'error': 'Invalid JSON supplied'}
 30         print('Payload received {0}'.format(json_payload))
 32         return {'Payload Received Success'}
 33 
 34 
 35 def main():
 36     loop = asyncio.get_event_loop()
 37     server = Sample(hostname='127.0.0.1', loop=loop)
 38     srv = loop.run_until_complete(loop.create_server(
 39         server.make_handler, '127.0.0.1', 8080))
 40     print('Server listening on port 8080')

 44     try:
 45         loop.run_forever()
 46     except KeyboardInterrupt:
 47         pass
 48     finally:
 49         srv.close()
 50         loop.run_until_complete(srv.wait_closed())
 51         loop.close()
 52 
 53 
 54 if __name__ == '__main__':
 55     main()

1 Answer 1

1

The reason why code is not working is a simple typo in the Sample class constructor _init__ instead of __init__. After fixing this everything should work as expected.

edit

The handler method should expect only request argument, body can be obtained from passed request.

def post_handler(self, request):
    payload = request._request_body.decode('utf-8')
    if not payload:
        return {'error': 'Payload not supplied'}

    try:
        json_payload = json.loads(payload)
    except ValueError:
        return {'error': 'Invalid JSON supplied'}
    print('Payload received {0}'.format(json_payload))
    return {'result': 'Payload Received Success'}

hint

By the way, the development of aiorest has stopped.

Sign up to request clarification or add additional context in comments.

3 Comments

I am able to send GET requests but my POST always send an empty response back when I curl it. I initialized payload to request.data. I think the data passed using CURL is not getting set to payload in my post_handler. Could you point, how to set payload in my post_handler from the request object ?Thanks
Never mind, I used aiohttp to get everything working.
Thanks. I ended up using payload = request.text() which returns a string and then json.loads(payload) since that's the valid request method available in aiohttp - aiohttp.readthedocs.org/en/stable/web_reference.html#request

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.