hailes
hailes

Reputation: 125

Python Requests POST returning incomplete content

I am trying to download a csv file using the Python Requests library. I am using the Requests library because I first must get past a Disclaimer page so I use the Session object to store all the cookies and all that jazz. My POST request keeps returning with a response content that is only the first 6 lines of the csv file. When I download the file using a browser, it is 1622 rows long. My current script:

import logging
logging.basicConfig(level=logging.DEBUG)
import pdb
import requests

s = requests.Session()

## Disclaimer page session
dis_url = 'http://a100.gov.bc.ca/pub/gwl/disclaimer.do'
accept_form = {'submitType':'Accept'}
s.post(dis_url, data=accept_form)


## POST request
base_url = 'http://a100.gov.bc.ca/pub/gwl/plot.do'
postContent = {
'fromYear':'2012',
'fromMonth':'1',
'fromDay':'1',
'toYear':'2013',
'toMonth':'1',
'toDay':'1',
'emsIDs':'E290172' ,
'mode':'GRAPH',
'mmaFlags':'false',    
'submitType':'Download'}


httpHeaders = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Host': 'a100.gov.bc.ca',
'Connection': 'keep-alive',
'Content-Length': '155',
'User-Agent': 'python-requests/1.2.3 CPython/2.7.3 Linux/3.5.0-23-generic',
'Content-Type': 'application/x-www-form-urlencoded',
'Referer': 'http://www.env.gov.bc.ca/wsd/data_searches/obswell/map/obsWells.html',
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Language': 'en-US,en;q=0.8'}


r = s.post(base_url, data=postContent, headers=httpHeaders, stream=False, timeout=3600)
print r.content

I should also mention that I have also tried to return the csv via chunking like so:

with open("report.csv",'wb') as file:
    r = s.post(base_url,stream=True,timeout=3600, data=postContent, headers=httpHeaders)
    for chunk in r.iter_content(chunk_size=1024):
        if chunk:
            file.write(chunk)
            file.flush()

But I still only get the first 6 lines in the report.csv.

I'm thinking that my content isn't being fully loaded because I am missing something in my request header. Here is the (working) browser request header:

POST /pub/gwl/plot.do HTTP/1.1
Host: a100.gov.bc.ca
Connection: keep-alive
Content-Length: 155
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: http://www.env.gov.bc.ca
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://www.env.gov.bc.ca/wsd/data_searches/obswell/map/obsWells.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.    
Cookie: JSESSIONID=73de9312c8dcf1c4c657d19adbe811b88792479fe72eb2e2feeedea7d88bdbf8.e3uMah8KbhmLe3mMbN8Pa3uPbi1ynknvrkLOlQzNp65In0; WT_FPC=id=2fcd604924a9af3c13e1374599088181:lv=1383612362138:ss=1383612301792

And the working browser response header:

HTTP/1.1 200 OK
Date: Mon, 04 Nov 2013 20:59:52 GMT
Server: Oracle-Application-Server-10g/10.1.2.2.0 Oracle-HTTP-Server
Content-Disposition: attachment; filename="gwl_report.csv"
Cache-Control: must-revalidate
Content-Type: application/download
Set-Cookie: JSESSIONID=61d874e1b5ce07df96aaabe504d7c18788e5aaf773a7bee7ab4b0cf349a88aaa.e3uMah8KbhmLe3mMbN8Pa3uPbi1ynknvrkLOlQzNp65In0; path=/pub/gwl
Transfer-Encoding: chunked

The request headers from the python post request (my response is missing Transfer-Encoding: chunked):

Content-Length : 126
Accept-Language : en-US,en;q=0.8
Accept-Encoding : gzip,deflate,sdch
Connection : keep-alive
Accept : text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent : python-requests/1.2.3 CPython/2.7.3 Linux/3.5.0-23-generic
Host : a100.gov.bc.ca
Referer : http://www.env.gov.bc.ca/wsd/data_searches/obswell/map/obsWells.html
Cookie : JSESSIONID=9a51e637cccc6164e4784631ef9a0ab21574c518c1c5c86cf0892bbf2aa22c95.e3uMah8KbhmLe3mMbN8Pa3uPbi1ynknvrkLOlQzNp65In0
Content-Type : application/x-www-form-urlencoded

Python response header (mine is missing Transfer-Encoding: chunked):

content-length : 200
content-disposition : attachment; filename="gwl_report.csv"
set-cookie : JSESSIONID=9a51e637cccc6164e4784631ef9a0ab21574c518c1c5c86cf0892bbf2aa22c95.e3uMah8KbhmLe3mMbN8Pa3uPbi1ynknvrkLOlQzNp65In0; path=/pub/gwl
server : Oracle-Application-Server-10g/10.1.2.2.0 Oracle-HTTP-Server
cache-control : must-revalidate
date : Tue, 05 Nov 2013 00:51:47 GMT
content-type : application/download

Does anyone know how I can make a POST request that will return the whole csv file?

Upvotes: 3

Views: 2430

Answers (1)

Related Questions