Search code examples
pythoncsvhttppostsanitization

Read a CSV file and make HTTP POST from each line with Python


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


Solution

  • 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)