HTTPS POST request Python

https, post, python, urllib, urllib2

Solution

The `BadStatusLine: '' (in httplib.py)` gives away that there might be something else going on here. This may happen when the server sends no reply back at all, and just closes the connection.

As you mentioned that you're using an SSL connection, this might be particularly interesting to debug (with `curl -v URL` if you want). If you find out that `curl -2 URL` (which forces the use of SSLv2) seems to work, while `curl -3 URL` (SSLv3), doesn't, you may want to take a look at issue #13636 and possibly #11220 on the python bugtracker. Depending on your Python version & a possibly misconfigured webserver, this might be causing a problem: the SSL defaults have changed in v2.7.3.

Problem

I want to make a post request to a HTTPS-site that should respond with a .csv file. I have this Python code: ``` url = 'https://www.site.com/servlet/datadownload' values = { 'val1' : '123', 'val2' : 'abc', 'val3' : '1b3', } data = urllib.urlencode(values) req = urllib2.Request(url,data) response = urllib2.urlopen(req) myfile = open('file.csv', 'wb') shutil.copyfileobj(response.fp, myfile) myfile.close() ``` But 'm getting the error: ``` BadStatusLine: '' (in httplib.py) ``` I've tried the post request with the Chrome Extension: Advanced REST client (screenshot) and that works fine. What could be the problem and how could I solve it? (is it becasue of the HTTPS?) EDIT, refactored code: ``` try: #conn = httplib.HTTPSConnection(host="www.site.com", port=443) ``` => Gives an `BadStatusLine: ''` error ``` conn = httplib.HTTPConnection("www.site.com"); params = urllib.urlencode({'val1':'123','val2':'abc','val3':'1b3'}) conn.request("POST", "/nps/servlet/exportdatadownload", params) content = conn.getresponse() print content.reason, content.status print content.read() conn.close() except: import sys print sys.exc_info()[:2] ``` Output: ``` Found 302 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <HTML><HEAD> <TITLE>302 Found</TITLE> </HEAD><BODY> <H1>Found</H1> The document has moved <A HREF="https://www.site.com/nps/servlet/exportdatadownload">here</A>.<P> <HR> <ADDRESS>Oracle-Application-Server-10g/10.1.3.5.0 Oracle-HTTP-Server Server at mp-www1.mrco.be Port 7778</ADDRESS> </BODY></HTML> ``` What am I doing wrong?

Original source

Related problems