Home > Software design >  Read a CSV file and make HTTP POST from each line with Python
Read a CSV file and make HTTP POST from each line with Python

Time:12-23

I have a CSV file witch I want to read each line and match the header to the value to make a HTTP POST to a web server. Here is an example of a CSV file:

"Item Name","OS","Serial Number","Asset Tag","Manufacturer","Model Name","Model Number","Category","IP","Status"
"MYCOMPUTER","Microsoft Windows 10 Pro","SOMETHING","SOMETHING","Dell","Latitude ","5420","Desktop","0.0.0.0","Ready"

Here is my python code:

    import requests
import csv
import time
import pandas as pd

url = "https://develop.snipeitapp.com/api/v1/hardware"

headers= {
    "Accept": "application/json",
    "Content-Type": "application/json"
}


df = pd.read_csv('IMPORT.csv')
for idx, data in df.iterrows():
    payload = {
    "status_id", data['Status'],
    "model_id", data['Model Number'],
    "name", data['Item Name'],
    "model_name", data['Model Name'],
    "serial", data['Serial Number'],
    "os", data['OS'],
    "manufacturer", data['Manufacturer'],
    "ip", data['IP'],
    "Category", data['Category']
    
}
    response = requests.request("POST", url, json=payload, headers=headers)

But I want to make a post for each line in a CSV file with the values matching the header per payload filed if that makes sense.

UPDATE I updated my code in this post and now I get this error:

TypeError: Object of type set is not JSON serializable

Thanks beforehand! Best regards Max

CodePudding user response:

You have multiple options but all have one in common, you need to open the file and iterate over the rows.

You can open the file using the csv module, using pandas or just via the open statement:

HEADERS= {
    "Accept": "application/json",
    "Content-Type": "application/json"
}

1 Pandas

import pandas as pd
df = pd.read_csv('csv_path_here')
for idx, data in df.iterrows():
    payload = {'asset_tag', data['asset_tag'] .....}
    response = requests.request("POST", url, json=payload, headers=headers)
    # do stuff here

2 Using csv from here

import csv
rows = []
with open("Salary_Data.csv", 'r') as file:
    csvreader = csv.reader(file)
    header = next(csvreader)
    for row in csvreader:
        rows.append(row)

#Iterate now over rows
For row in rows[1:]:
    payload = {'asset_tag', row[0] .....}
    response = requests.request("POST", url, json=payload, headers=headers)

CodePudding user response:

Hi you need a data structure to hold all the csv data for this use Pandas. First install pandas in python you can install that by following command.

pip install pandas

After that import that package and use pandas.read_csv to read the csv and get that to a Pandas Data frame. Now iterate over the Data frame and make the post request. Sample code below.

import pandas as pd

df = pd.DataFrame({'c1': [10, 11, 12], 'c2': [100, 110, 120]})

for index, row in df.iterrows():
    print(row['c1'], row['c2'])
  • Related