0

I have a wordpress site with a form in it from Gravity Forms. In some of the form fields, users are required to upload a file. I'm using the GF API to retrieve the form entries, specifically the fields where users had to upload files, with python's urllib.urlopener.

Similar to this post, I try retrieving and writing the file to my local drive, but the problem is I get this as an error:

IOError: ('http error', 403, 'Forbidden', <httplib.HTTPMessage instance at 0x0000000004770C88>)

This only happens with the URLs of the uploaded files from the entries. All other public PDFs work and I'm able to write them to my drive with response.content.

I've added headers to the urllib.URLopener.addheader object. But why am I forbidden?

parsa_047Fletcher
  • 197
  • 1
  • 1
  • 8

1 Answers1

0

The issue wasn't that I had forbidden access. I knew that from the get go.

The fix is to use requests as shown in the second example of the top solution here but add your headers in dict format like so:

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36','Content-Type': 'application/pdf'}
r = requests.get(url, headers=headers)
with open(saver, 'wb') as f:
    f.write(r.content)

urllib don't work... still don't know why though.

parsa_047Fletcher
  • 197
  • 1
  • 1
  • 8