1

I have an image file on a server that is protected by Windows Authentication which I have to access. I used the requests_ntlm module of python to access the file with Windows Auth, which I was able to. However, on sending this data back as an image, I am getting an error.

The sample code which I am using follows:

def sendImage(imagename):
  """
  Takes image name as a parameter and get the file from the XYZ server 
  after Windows Authentication and serve the image as a response
  """
  
  username = USERNAME
  password = PASSWORD

  r = requests.get( BASE_URL + str(imagename), auth=HttpNtlmAuth(username, password))

  return send_file(
      io.BytesIO(r.content),
      mimetype='image/jpg'
  )

The traceback of error which I am getting follows:

Traceback (most recent call last):
  File "/home/pyenv/local/lib/python2.7/site-packages/werkzeug/wsgi.py", line 704, in __next__
    return self._next()
  File "/home/pyenv/local/lib/python2.7/site-packages/werkzeug/wrappers.py", line 81, in _iter_encoded
    for item in iterable:
TypeError: 'Response' object is not iterable

I have checked that the response content with bytesIO is getting opened when I am using it with PIL, so there is so problem with the image that I am receiving.

I am unable to figure out the cause of this error.

Naman Sharma
  • 179
  • 1
  • 1
  • 12
  • maybe this thread could give you hints https://stackoverflow.com/a/13137873/12368419 – cizario Jun 29 '20 at 09:21
  • @cizario: I looked into the link and it describes downloading the file. However, I am able to access the image, the problem that I am facing is that I am unable to throw this image back in the API due to some error in Response. – Naman Sharma Jun 29 '20 at 09:45

1 Answers1

-1

response= are only need string value, so it becomes:

  return send_file(
      r.text,
      mimetype='image/jpg'
  )
  • The documentation of send_file specifically says that the parameter can either be a filename or file object, thus string value won't work. Documentation link: https://flask.palletsprojects.com/en/1.1.x/api/#flask.send_file – Naman Sharma Jun 29 '20 at 09:36