I want to read data from a CSV file and parse that into Python POST requests in a dictionary format which will create a content-type: multipart/form-data auto
when sending a request to the API.
I have tried
with open("listingData1.csv", "r") as f_input:
csv_input = csv.DictReader(f_input)
for row in csv_input:
data_android = {
'mailing_details': row["shipping"],
'caroupay':'false',
'price':row['price'],
'description':row['desc'],
'title':row['title'],
'meetup':'false',
'condition':'2',
'mailing':'true',
'collection_id':row['cat']
}
print(row)
search_path = row['image']
urls = []
for file in os.listdir(os.getcwd()+search_path):
if file.endswith((".jpg",".jpeg",".png",".JPG",".JPEG",".PNG")):
x = os.getcwd()+"\\"+search_path+"\\"+file
urls.append(x)
files = {'photo_%s' % x: ('photo_%s.jpg' % x, open(file, 'rb'), 'image/jpeg') for x, file in enumerate(urls)}
#print data_android
response = requests.request("POST", url_and,data=data_android,headers=headers)
print(response.text.encode("utf-8"))
However, requests send data to the API with content-type:application/x-www-form-urlencoded
which is not accepted by the API:
'POST /api/3.0/listings/ HTTP/1.1\r
Host: api.testdomain.com\r
Connection: keep-alive\r
Accept-Encoding: gzip, deflate\r
Accept: */*\r
User-Agent: python-requests/2.21.0\r
platform: android\r
Authorization: Token afs7dfa76sd8f7a68sd76f8as6fd8s\r
Content-Length: 179\r
Content-Type: application/x-www-form-urlencoded\r
\r
abcpay=FALSE&description=test1&mailing_details=FREE&price=239.00&title=Pet+Cat+Dog+Carrier+Portable+Breathable+Comfortable&meetup=FALSE&collection_id=45&mailing=TRUE&condition=2'
After that, I tried to use the default dictreader option from CSV module:
with open("listingData1.csv", "r") as f_input:
csv_input = csv.DictReader(f_input)
for row in csv_input:
search_path = row['image']
urls = []
for file in os.listdir(os.getcwd()+search_path):
if file.endswith((".jpg",".jpeg",".png",".JPG",".JPEG",".PNG")):
x = os.getcwd()+"\\"+search_path+"\\"+file
urls.append(x)
files = {'photo_%s' % x: ('photo_%s.jpg' % x, open(file, 'rb'), 'image/jpeg') for x, file in enumerate(urls)}
#print data_android
response = requests.request("POST", url_and,data=row,headers=headers)
print(response.text.encode("utf-8"))
which also gives the same output. Now, is there any way to parse CSV data into POST requests as a dictionary file?
Here is my full code:
import requests
import logging
import os
import json
import csv
try:
import http.client as http_client
except ImportError:
# Python 2
import httplib as http_client
http_client.HTTPConnection.debuglevel = 1
# You must initialize logging, otherwise you'll not see debug output.
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 = '8fas8d7f9a8s7d9f87as9d8f7sa9df7s9f'
headers = {
'Authorization': "Token " + android_token,
'platform': 'android',
}
data_android = {}
with open("listingData1.csv", "r") as f_input:
csv_input = csv.DictReader(f_input)
for row in csv_input:
data_android = {
'mailing_details':row['shipping'],
'abcoupay':'false',
'price':row['price'],
'description':row['desc'],
'title':row['title'],
'meetup':'false',
'condition':'2',
'mailing':'true',
'collection_id':row['cat']
}
search_path = row['image']
urls = []
for file in os.listdir(os.getcwd()+search_path):
if file.endswith((".jpg",".jpeg",".png",".JPG",".JPEG",".PNG")):
x = os.getcwd()+"\\"+search_path+"\\"+file
urls.append(x)
files = {'photo_%s' % x: ('photo_%s.jpg' % x, open(file, 'rb'), 'image/jpeg') for x, file in enumerate(urls)}
response = requests.request("POST", url_and,data=data_android,headers=headers)
print(response.text.encode("utf-8"))