Python Forum
Problem with CSV download - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Problem with CSV download (/thread-14024.html)



Problem with CSV download - imtiazu - Nov-11-2018

Hey there - very very new to Python and trying to knock up a script that posts to an API.
The core of the script works fine and I can successfully post to an API, but the API returns a csv, not to be confused with a CSV URI. It's identical to the web browser download once the csv is ready.

The issue is that Python seems to be trying to read the download into memory, not the actual CSV file. Does that make sense?

The code I'm firing is this..

exporturl = "https://website.com/Inquiries/ExportToCsv"

exportpayload = 'searchOptions={"filmCode":"","From":"10/11/2018","To":"11/11/2018","TransactionNumber":"","SessionFrom":"","SessionTo":"","TransactionType":"All","BookingReference":"","itemCode":"","ticketCode":"","userCode":"","workstationCode":"","CardNumberFirstSixDigits":"","CardNumberLastFourDigits":"","BookingEmail":"","MembershipCardNumber":"","PaymentType":"","BookingName":""}'

exportheaders = {
'origin': "https://website.com",
'upgrade-insecure-requests': "1",
'content-type': "application/x-www-form-urlencoded",
'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",
'accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
'referer': "https://website.com/inquiries/transactions",
'accept-encoding': "gzip, deflate, br",
'accept-language': "en-US,en;q=0.9,es;q=0.8",
'cache-control': "no-cache",
}


response = session.post(exporturl, cookies=session.cookies.get_dict(), headers=exportheaders, data=exportpayload, verify=False)

Python waits for the response and then just displays garbage - which is basically the attempted download of the csv file.
If I run the exact same thing via Postman it works fine and the file is downloaded via the web browser in the background.

Any pointers would be great. I've tried to write out the data to file, which generates a file but it's the same garbage which would be displayed if I wrote it to console.

Thanks
Immy
Python 3.7


RE: Problem with CSV download - SheeppOSU - Nov-11-2018

This should make it easier to read
exporturl = "https://website.com/Inquiries/ExportToCsv"

exportpayload = 'searchOptions={"filmCode":"","From":"10/11/2018","To":"11/11/2018","TransactionNumber":"","SessionFrom":"","SessionTo":"","TransactionType":"All","BookingReference":"","itemCode":"","ticketCode":"","userCode":"","workstationCode":"","CardNumberFirstSixDigits":"","CardNumberLastFourDigits":"","BookingEmail":"","MembershipCardNumber":"","PaymentType":"","BookingName":""}'

exportheaders = {
'origin': "https://website.com",
'upgrade-insecure-requests': "1",
'content-type': "application/x-www-form-urlencoded",
'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",
'accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
'referer': "https://website.com/inquiries/transactions",
'accept-encoding': "gzip, deflate, br",
'accept-language': "en-US,en;q=0.9,es;q=0.8",
'cache-control': "no-cache",
}


response = session.post(exporturl, cookies=session.cookies.get_dict(), headers=exportheaders, data=exportpayload, verify=False)



RE: Problem with CSV download - snippsat - Nov-12-2018

(Nov-11-2018, 09:17 PM)imtiazu Wrote: Python waits for the response and then just displays garbage - which is basically the attempted download of the csv file.
That garbage can be binary data of csv file,so try to save content.
import requests

with open("data.csv",'wb') as f_out:
    f_out.write(response.content)