0

I receive a Chunked Encoding Error when running my script using python requests, and I am wondering why I could be receiving this error.

When I do resp.encoding I get it back None

I am currently not using any SSL certification verification in my script and so I get Insecure Requests Warnings as well.

    for attachment in attachments:
     if attachment['type'] == 'Form':
        continue
      # create insertcursor row
      row = cursorPointData.newRow()
      pprint(attachments)

      for key in attachment:
        pprint(key)
        pprint(attachment[key])

        row.setValue(key, attachment[key])

    # Download file

      guid = attachment['guid']
      resp = requests.get(
        'https:...' + guid,
        headers={'Authorization': 'Bearer ' + token},
        stream=True,
        verify=False
      )
     contentType = resp.headers['Content-Type']
     contentLength = int(resp.headers['content-length'])
     pprint('contentLength = ' + str(contentLength))
     extension = contentType.split('/')[1]
     filename = '{0}.{1}'.format(guid, extension)
     output_path = os.path.join(attachmentDir, filename)
     attachmentPath = os.path.join("filepath", filename)

    with open(output_path, 'wb') as f:
        for chunk in resp.iter_content(chunk_size=None):
            if chunk:
                f.write(chunk)
                f.flush() #flush the data to file
                os.fsync(f.fileno()) #force the file write and free memory

    row.setValue('attachmentPath', attachmentPath)

    cursorPointData.insertRow(row)
del cursorPointData, row
mwinds
  • 11
  • 3

1 Answers1

1

I checked in the Network tab of Chrome developer tools, and I was getting a status 200. However in Firefox developer tools, I got a 301 status: "Moved Permanently".

It turns out I was putting in the wrong url to the request and needed to change the url to the updated version it was redirecting to.

Found out you can use response.history in the python requests library to find any redirection issues.

Now, I get back utf-8 as the response encoding instead of none.

Leaving this here in case anyone else runs into the same issue.

mwinds
  • 11
  • 3