The situation is that I have got someone's code who left my office. He has used Sanic framework. Now, the client has requested some changes. I have to send an additional data to server which will be stored in the database and later can be retrieved.
Code:
The code earlier was:
def send_update_to_server(v_Number, state, server_url):
data = {'state': state}
address_to_send = "http://{}/vote/{}".format(server_url, v_Number)
r = requests.post(address_to_send, json=data)
So, to send the additional data, I added the variable i want to send:
def send_update_to_server(v_Number, state, server_url, new_number):
data = {'state': state}
address_to_send = "http://{}/vote/{}".format(server_url, v_Number)
r = requests.post(address_to_send, new_number, json=data)
The post method on Sanic side is:
async def post(self, request, *args, **kwargs):
//some_code
Now, I am not able to retrieve the new_number that I sent from my code using requests.post. Can anyone help me in this? I am pretty new to server side programming and especially Sanic. I would be glad to provide additional code if required.
All of the json data sent to a Sanic endpoint is available as request.json
, see docs.
So, it looks like you just need to do something like this:
def send_update_to_server(v_Number, state, server_url, new_number):
data = {'state': state, 'new_number': new_number}
address_to_send = "http://{}/vote/{}".format(server_url, v_Number)
r = requests.post(address_to_send, json=data)
Then, you can read it in your handler:
from sanic.response import text
async def post(self, request, *args, **kwargs):
return text(request.json.get("new_number"))