Sending messages via POST requests
So far we've made good progress; we have a site that has the ability to display all of the messages in our data store with two microservices. One microservice handles the storing and retrieval of our messages, and the other acts as a web server for our users. Our MessageService
already has the ability to save messages; let's expose that in our WebServer
via a POST
request.
Adding a send messages POST request
In our service.py
, add the following import:
import json
Now add the following to our WebServer
class:
@http('POST', '/messages') def post_message(self, request): data_as_text = request.get_data(as_text=True) try: data = json.loads(data_as_text) except json.JSONDecodeError: return 400, 'JSON payload expected' try: message = data['message'] except KeyError: return 400, 'No message given' self.message_service.save_message(message) return 204, ''
With our new POST
entrypoint, we...