Home > Back-end >  Why Is My Django Post Request Sending Incorrectly Parsed My List of Dictionaries?
Why Is My Django Post Request Sending Incorrectly Parsed My List of Dictionaries?

Time:02-14

Here is my Django view:

def sendMail(request):
    url = 'https://example.com/example'
    dictwithlist = request.FILES['dictwithlist']
    parsed = json.load(dictwithlist)
    
    // the log file shows that the lists are intact after json.loads
    logip(request, json.loads(parsed))
    x = requests.post(url, json.loads(clockoutJSON))
return HttpResponse(status=204)

If I just send parsed data my express server receives an empty dict, {}. When I log the json.loads(parsed) I find good data, with the lists intact. When the data gets to the other side though, the dictionaries inside the nested list are all removed, replaced by only strings of their keys.

I tried using headers as described here: Sending list of dicts as value of dict with requests.post going wrong but I just get 500 errors. I don't know if I'm formatting the headers wrong or not. (because the code has line spacing and I'm copying it)

Can anyone help me understand why this is failing? I need that list to get through with its dictionaries intact.

CodePudding user response:

I believe you may need to use dumps rather than loads when sending the request:

x = requests.post(url, json.dumps(parsed))

Alternatively if you're using the python requests library you can send json as a dict as below:

response = requests.post(url=url, json=parsed)
  • Related