0
votes

The purpose of this script is to connect to an API endpoint that provides a .CSV file. I'm not trying to manipulate the data or do anything with the information other than grab it from the endpoint and then save the file to a local directory

I've tested the endpoint and can get a 200 HTTP response from it, but I can not open the file or save it. I continue to get a typeerror response for the last line of the code.

import urllib.request as urllib2
import requests
from requests.auth import HTTPBasicAuth
import csv

url = "Https://API_LINK/Details/full_csv"
request = urllib2.Request(url)
request.add_header('header_details', 'Token token="token_details"')
response = urllib2.urlopen(request)
with open(response,'w', newline='') as f:

I expected to be able to define the opened file as 'f', then pass 'f' to csv.reader() and then view rows from the file. What do I do to pass the info as bytes and not as just an httpResponse?

1
Why are you importing requests but only using urllib2? - clubby789
I was having trouble with the urllib package at first. Should I adjust the packages? - Ryan
Well, don't import a package you're not using. And I've generally seen it suggested that requests is better than urllib2, so you might consider using that instead. - clubby789
sorry - i have a part of the script that I didn't post that is to send the request through my company's proxy. That's why I have urllib2. I see now how that could be confusing. - Ryan

1 Answers

0
votes

https://docs.python.org/2/howto/urllib2.html

Calling .read() on the response will return the fetched text from the page.