I am trying to send POST data from a python script to a url. Here is the code:
def createperson(self,name=None):
SERVER_BASE_ADDR = 'http://localhost:8000/'
postdata = urllib.urlencode({'name':name})
req = urllib2.Request(SERVER_BASE_ADDR + 'people/add')
fd = urllib2.urlopen(req,postdata)
return name
But this returns
HTTPError: HTTP Error 500: INTERNAL SERVER ERROR
Adding a person on the url is fine. Any ideas?
EDIT
The server side uses django decorators to handle POST and GET.
@require_http_methods(["GET", "POST"])
def add(request):
if request.method == "POST":
#do stuff with post data
Isn’t the above python code the same as submitting a form on a html template? If its the same then shouldn’t the handlers handle it the same way?
Thank you
Solved by using:
c = urllib2.build_opener()
postdata = urllib.urlencode({'name':name})
r = c.open(url,postdata)
A different approach but does the job. Thank you for all your answers.
The HTTP 500 error means that the problem doesn’t occur on the client-side python script, but on the server side.
Check your server side code, maybe it reads data only from GET, not POST.