Marc Blase

Process CSV file and curl request with its contents

#!/bin/bash
# Purpose: Read Comma Separated CSV File and cURL using contents

INPUT=$1
OLDIFS=$IFS
IFS=','
[ ! -f $INPUT ] && { echo "Please provide a CSV file"; exit 99; }
while read user_email first_name last_name Level start_issue start_date
do
    # curl request with contents from CSV and write output to file
    curl -m 10 -X POST --data API_Key=KEY_GOES_HERE --data Email=$user_email --data Program_Data=PROGRAM_DATA_GOES_HERE --data FirstName=$first_name --data LastName=$last_name https://API_ENDPOINT.TLD >> curl_output.json
	
done < $INPUT
IFS=$OLDIFS

Run this as, if saved as curl-csv.sh:

$ ./curl-csv.sh curl-csv.csv

If you get the “/bin/bash^M: bad interpreter: No such file or directory” error message you probably created the .sh file in Windows and are running in a Linux shell. Run the following command to rectify.

$ sed -i -e 's/\r$//' curl-csv.sh
Published on December 3, 2020