pythonexpress

Node server doesnt receive the JSON file I send him


i'm trying to send a post request to my express server using python, this is my code:

import requests


print("started");
URL = "http://localhost:5000/api/chat"

PARAMS = {'ID':"99","otherID":"87",'chat':[{"senderName":"tom","text":"helloworld"}]}

r = requests.post(url = URL, data = PARAMS)

pastebin_url = r.text 
print("The pastebin URL is:%s"%pastebin_url) 

but when I recieve the call, I get an empty object on my Node server, am I missing something? (With postman it works fine so its not the server)


Solution

  • typically requests library differentiates on the type of request depending on the param used to make the request. Meaning if you intend to make a JSON post then one should use the json param like so:

    response = requests.post(url=URL, json=PARAMS)
    

    what this does is set the accompanying headers which is why when your express server attempts to parse it, it comes back empty