in Education by
What is the problem Here ? I got an error shows ValueError: too many values to unpack This code process is to get all available images in a folder then put that images location into an array. Then create an other array called files and add image location with specific format and send the POST request to the API import requests import logging import os import json try: import http.client as http_client except ImportError: # Python 2 import httplib as http_client http_client.HTTPConnection.debuglevel = 1 logging.basicConfig() logging.getLogger().setLevel(logging.DEBUG) requests_log = logging.getLogger("requests.packages.urllib3") requests_log.setLevel(logging.DEBUG) requests_log.propagate = True url_and = "https://api.test.com/api/3.0/listings/" android_token = '68as76df87s86df7asd76f87as6df78sfd' headers = { 'Authorization': "Token " + android_token, 'platform': 'android' } data_android = { 'mailing_details':'3', 'abcoupay':'false', 'price':'55.00', 'description':'Test description', 'title':'TEST drill machine and others.', 'meetup':'false', 'condition':'2', 'mailing':'true', 'collection_id':'24' } urls = [] for file in os.listdir(os.getcwd()+"/product_images"): if file.endswith((".jpg",".jpeg",".png",".JPG",".JPEG",".PNG")): x = os.getcwd()+"\\"+file urls.append(x) files = [] x = 0 for file in urls: files.append("'photo_"+str(x)+"': ('image_"+str(x)+".jpg', open('"+file+"', 'rb'), 'image/jpeg')") x+=1 # files = { # 'photo_0': ('image_0.jpg', open('E:/products files/Drill machine/1.jpg', 'rb'), 'image/jpeg'), # 'photo_1': ('image_1.jpg', open('E:/products files/Drill machine/2.jpg','rb'), 'image/jpeg') # } response = requests.request("POST", url_and,data=data_android,files=files,headers=headers) print(response.text.encode("utf-8")) Error Traceback (most recent call last): File "cookies.py", line 102, in response = requests.request("POST", url_and,data=data_android,files=files,headers=headers) File "C:\python27\lib\site-packages\requests\api.py", line 60, in request return session.request(method=method, url=url, **kwargs) File "C:\python27\lib\site-packages\requests\sessions.py", line 519, in request prep = self.prepare_request(req) File "C:\python27\lib\site-packages\requests\sessions.py", line 462, in prepare_request hooks=merge_hooks(request.hooks, self.hooks), File "C:\python27\lib\site-packages\requests\models.py", line 316, in prepare self.prepare_body(data, files, json) File "C:\python27\lib\site-packages\requests\models.py", line 504, in prepare_body (body, content_type) = self._encode_files(files, data) File "C:\python27\lib\site-packages\requests\models.py", line 141, in _encode_files for (k, v) in files: ValueError: too many values to unpack JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The files parameter for requests.request should be a dict where the keys are file names and the values are file content or file objects. You should therefore build your files variable as a dict instead: files = {'image_%s.jpg' % x: open(file, 'rb') for x, file in enumerate(urls)} If you need the content type as well, you can make the dict values 3-tuples that contain the file name, the file object and the content type: files = {'image_%s.jpg' % x: ('image_%s.jpg' % x, open(file, 'rb'), 'image/jpeg') for x, file in enumerate(urls)}

Related questions

0 votes
    I'm scraping some internal pages using Python and requests. I've turned off SSL verifications and warnings. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    I'm using the following method to send mail from Python using SMTP. Is it the right method to use ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    I'm using the following method to send mail from Python using SMTP. Is it the right method to use ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    I'm using the following method to send mail from Python using SMTP. Is it the right method to use ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    I am receiving the error: ValueError: Wrong number of items passed 3, placement implies 1, and I am struggling to ... 'sigma'] = sigma Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    def multiple_dfs(xyz_file, sheet, *args): row=2 writer = pd.ExcelWriter(xyz_file, engine='openpyxl') df = pd. ... help me over this? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I have a dataset that only contains y-values in one column. I want to insert a column of x- ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I have an array of the shape (100000, 1) with each element in an array of type positive integer and not ... help would be appreciated. Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
0 votes
    I've been given to understand that Python is an interpreted language... However, when I look at my Python source ... do these come in? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I want to change a couple of files at one time, iff I can write to all of them. I'm wondering if I ... to this problem look like? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    How do I import other files in Python? How exactly can I import a specific python file like import file.py? How ... gap from extra.py? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    Can someone tell me how to find all the files in directory with .txt extension in Python? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I want to save files for each result from the loop. For example, I wrote the code but it only saves one file ... do I modify my code? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have 2 data frames df1 Name 2010 2011 0 Jack 25 35 1 Jill 15 20 df2 Name 2010 2011 0 Berry 45 25 1 ... used the code df1.add(df2) Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
0 votes
    In python, I want to sum all of the values in a variable. My code is x=100 y=200 for i in range(a,b) ... of all numbers which is 7500. Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
...