Reputation: 1793
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.
Upvotes: 0
Views: 2205
Reputation: 12543
HTTP 500 errors are definitely internal server errors. The problem occurs on the server, as @dlutxx has pointed out.
To debug this further, I think you should examine your Django handler. If it really does work for GET requests but not POST requests, perhaps you should look at what is different between the GET handler and the POST handler.
import logging
logger = logging.getLogger(__name__)
@require_http_methods(["GET", "POST"])
def add(request):
try:
if request.method == "POST":
# Something is probably broken here.
elif request.method == "GET":
# But apparently works fine here.
except:
# Why don't we insert some debugging output to try to catch it.
logger.exception("Problem in add handler")
raise
EDITed to use Python logging framework (per Django docs) instead of stdout.
Upvotes: 2
Reputation: 5999
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.
Upvotes: 2