1

I am trying to send an excel file to server in POST Request in multipart/form-data content-type. I am getting an error:

too many values to unpack

What could be the reason? Below is the request what I am trying:

#Data = get_data('C:\foo.xls')
#print Data
Data = open('C:\foo.xls', 'rb')
print Data

headers = {
       'access-control-allow-origin': '*',
       'accept': 'application/json',
       'content-type': 'multipart/form-data', 
       'authorization': 'Basic xxxxxxxxx'
      }   
 R = requests.post('http://testserver:8080/v1/readyapi/executions/'+executionId+'/files', headers=headers, params=params, files=Data)
 print R.content

here is the error:

Traceback (most recent call last):
    (body, content_type) = self._encode_files(files, data)
  File "C:\Python27\lib\site-packages\requests\models.py", line 132, in _encode_files
    for (k, v) in files:
ValueError: too many values to unpack

I could not figure out by myself. tried few things, didn't work. Can someone please advise?

2 Answers 2

2

Try the following code

Data = open('C:\foo.xls', 'rb') 
headers = {
       'access-control-allow-origin': '*',
       'accept': 'application/json',
       'content-type': 'multipart/form-data', 
       'authorization': 'Basic xxxxxxxxx'
      }
 files = {"file_name": Data}
 url = 'http://testserver:8080/v1/readyapi/executions/'+executionId+'/files'
 R = requests.post(url, headers=headers, params=params, files=files)
 print R.content

You must have to pass the files argument as a dictionary or you can also try like below

files = {'file': ('foo.xls', open('foo.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}

Reference: http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file

Sign up to request clarification or add additional context in comments.

1 Comment

Tried it. Getting "Bad Request" <p>Problem accessing /v1/readyapi/executions/4d1c7e9b-55c8-4c9b-8f91-42aec0c29a37/files. Reason: <pre> Bad Request</pre></p>
0

Try this, right MIME type is important (https://wiki.selfhtml.org/wiki/MIME-Type/%C3%9Cbersicht)

headers = {
       'access-control-allow-origin': '*',
       'accept': 'application/json',
       'content-type': 'multipart/msexcel', 
       'authorization': 'Basic xxxxxxxxx'
      }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.