How to read CSV file and create User
I want to read and create user from csv file. but my code gave me MultiValueDictKeyError at /api/user/
read and crate user from CSV file
my Code is:
def read_and_create_csv_file():
header = {'Authorization': f'Token {get_token()}'}
url = f"{URL}/api/user/"
with open(r"E:\WC_API\employees.csv") as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
data = {
"USERNAME": row['username'],
"FIRST_NAME": row['FIRST_NAME'],
"LAST_NAME": row['LAST_NAME'],
"EMAIL": row["EMAIL"],
"PASSWORD": "Abc@123"}
response = requests.post(url, json=data, headers=header)
if response.status_code == 201:
print("User created successfully")
else:
print("Failed to create user")
read_and_create_csv_file()
this is the resule:
Failed to create user or MultiValueDictKeyError
Without seeing the csv file, my guess is that there is not a column named 'username', and there is a column named 'USERNAME'. Change line #8 accordingly.