Search code examples
pythondjangodictionarypython-requestsinternal-server-error

HTTPError: 500 while trying to post data using requests.post() in Django framework (python program)


Somebody Kindly help me out here. I'm trying to process few text files in a directory and convert them into dictionary. Then I'm trying to upload through the DJANGO REST API using requests.post() but I'm getting requests.exceptions.HTTPError: 500 Server Error: Internal Server Error for url: http://34.72.26.221/feedback.

My code is as follows:

 #!/usr/bin/env python3

import os
import requests
import glob

dirloc = r"/data/feedback/"
d = {}
for file in glob.iglob(dirloc+ '*'):
        if not file.startswith('.'):
            with open(file,'r') as f:
                d["title"] = f.readline().strip("\n")
                d["name"] = f.readline().strip("\n")
                d["date"] = f.readline().strip("\n")
                d["feedback"] = f.readline().strip("\n")
                print(d) #used for troubleshooting only#
                r = requests.put("http://34.72.26.221/feedback/", data = d)
                print(r.status_code)

I'm new to coding and still figuring out things. So this post may be missing some needed info. Please let me know if I need to add anything.


Solution

  • You're not using post you're using put with requets.put.

    Change your requests usage to:

    r = requests.post("http://34.72.26.221/feedback", data = d)