0

I have 2 django server. I want to send some POST data from the server A to server B.

I use this code on server A to send data (I simply follow the tutorial ) :

payload = {"contenu" : Contenu, "ID" : hashage} payload_json = json.dumps(payload, separators=(',', ': ')) with open('backend/config.json') as json_data: facto = json.load(json_data) json_data.close hostnamefacto = facto["Factory"]["IP"] portFacto = facto["Factory"]["port"] reponse = requests.post('http://'+hostnamefacto+':'+portFacto+'/outil/test/', data = payload_json) 

On server B, I use this code to get data :

try: contenu = request.POST['contenu'] except KeyError: contenu = None try: ID = request.POST['ID'] except KeyError: ID = None 

But ID and contenu are equal None. Does someone have an idea of how to do it ? Thanks a lot.

1 Answer 1

1

You're reading the POST parameters where you want the raw body parsed as JSON:

data = json.loads(request.body) id = data['ID'] 

Let the requests library do the JSON encoding for you:

payload = {"contenu" : Contenu, "ID" : hashage} r = requests.post('http://'+hostnamefacto+':'+portFacto+'/outil/test/', json=paylod) 
Sign up to request clarification or add additional context in comments.

2 Comments

I try what you told me, but I get an error. I solved it by replace json.load by json.loads and it works. Thank you.
Doh! those dang typos =)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.