1

I'm getting the following error:

ValueError: dictionary update sequence element #0 has length 1; 2 is required

And this is my code:

The SECRET_KEY_HERE is being replaced by my secret key.

import requests
url = 'https://app.nanonets.com/api/v2/ImageCategorization/UploadFile/'
data = {'file' :open('C:\\Users\\ch\\Desktop\\1_mONNI1lG9VuiqovpnYqicA.jpg', 'rb'),'category' :('Dog','Hamster','Cat','Rabbit'), 'modelId' :('', '756de8ac-9f09-4fba-87b0-4f30e7e8f12f')}

response = requests.post(url, auth= requests.auth.HTTPBasicAuth('SECRET_KEY_HERE', ''), files=data)
print(response.text)

This is the full Traceback:

 Traceback (most recent call last):
  File "nano.py", line 6, in <module>
    response = requests.post(url, auth= requests.auth.HTTPBasicAuth('', ''), files=data)
  File "C:\Python37\lib\site-packages\requests\api.py", line 116, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "C:\Python37\lib\site-packages\requests\api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Python37\lib\site-packages\requests\sessions.py", line 510, in request
    prep = self.prepare_request(req)
  File "C:\Python37\lib\site-packages\requests\sessions.py", line 453, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "C:\Python37\lib\site-packages\requests\models.py", line 316, in prepare
    self.prepare_body(data, files, json)
  File "C:\Python37\lib\site-packages\requests\models.py", line 504, in prepare_body
    (body, content_type) = self._encode_files(files, data)
  File "C:\Python37\lib\site-packages\requests\models.py", line 165, in _encode_files
    rf = RequestField(name=k, data=fdata, filename=fn, headers=fh)
  File "C:\Python37\lib\site-packages\urllib3\fields.py", line 69, in __init__
    self.headers = dict(headers)
ValueError: dictionary update sequence element #0 has length 1; 2 is required

This is my first time using python and I'm not sure what I'm doing wrong here, the code is copied from the Nanonets documentation page.

Could you point me in the right direction?

Kind Regards, Dave

Kek Lmao
  • 53
  • 6
  • Just to make sure, when executing the code, you are replacing the `'SECRET_KEY_HERE'` with your real key? – Hemerson Tacon Nov 01 '18 at 15:25
  • Yeah, the secret key is being replaced by my real key – Kek Lmao Nov 01 '18 at 15:29
  • This might help: https://stackoverflow.com/questions/14302248/dictionary-update-sequence-element-0-has-length-3-2-is-required – Sid Nov 01 '18 at 15:30
  • you may want to fuzz your traceback as it contains your secret key. – Landar Nov 01 '18 at 15:33
  • With the `requests` library, isn't `auth` arg just a 2-tuple of `(username, password)`? This error is what you get if you try to do dict-unpacking and one of the items in the sequence isn't a 2-tuple (it's expected to be a list of key, value pairs). – Two-Bit Alchemist Nov 01 '18 at 15:43
  • @Two-BitAlchemist How would I go about fixen that? I'm not known with python. I got assigned this task as an intern and its stressing me out :( – Kek Lmao Nov 02 '18 at 08:35

0 Answers0