Skip to content

Instantly share code, notes, and snippets.

@umamiMike
Last active November 22, 2017 23:27
Show Gist options
  • Save umamiMike/8eb40a2d0ab87053457e254b757f722b to your computer and use it in GitHub Desktop.
Save umamiMike/8eb40a2d0ab87053457e254b757f722b to your computer and use it in GitHub Desktop.
WIP make a batch series of ajax calls from a csv with the keys of the data as the headers of the first row
package main
import (
"encoding/json"
"fmt"
// "github.com/davecgh/go-spew/spew"
"github.com/recursionpharma/go-csv-map"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
)
type Header struct {
Type string `json:"type"`
Value string `json:"value"`
}
type Config struct {
Host string `json:"host"`
Endpoint string `json:"endpoint"`
Csvfile string `json:"csvfile"`
Headers []Header `json:"headers"`
}
func LoadConfig() (Config, error) {
var config Config
cfile, err := os.Open(os.Args[1])
defer cfile.Close()
if err != nil {
}
jsonParser := json.NewDecoder(cfile)
err = jsonParser.Decode(&config)
return config, err
}
func main() {
if len(os.Args) < 2 {
fmtString := `
{
"host" : "http://example.com",
"endpoint" : "/endpoint",
"csvfile": "/path/to/file.csv",
"headers" : [
{
"type" : "Cookie",
"value" : "PHPSESSID="
},
{
"type" : "Content-Type",
"value" : "application/x-www-form-urlencoded; charset=UTF-8"
},
{
"type" : "Origin",
"value" : "http://example.com"
}
]
}
*****************************************************************
`
fmt.Println("*************** you must supply the path to a config file in json format *******", fmtString)
return
}
processCsv()
}
func processCsv() {
confi, err := LoadConfig()
if err != nil {
fmt.Println("error loading config", err)
}
f, err := os.Open(confi.Csvfile)
if err != nil {
fmt.Println("Error:", err)
return
}
reader := csvmap.NewReader(f)
reader.Columns, err = reader.ReadHeader()
if err != nil {
fmt.Println(" error with ReadHeader", err)
os.Exit(1)
}
records, err := reader.ReadAll()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for _, v := range records { //v is the map we are going to parse into the values
data := url.Values{}
for i, j := range v {
buildData(&data, i, j)
}
performCall(&data)
}
}
func buildData(d *url.Values, k string, v string) {
d.Set(k, v)
}
func makeRequest(data *url.Values) *http.Request {
conf, err := LoadConfig()
if err != nil {
fmt.Println("error loading config", err)
}
req, _ := http.NewRequest("POST", conf.Host+conf.Endpoint, strings.NewReader(data.Encode()))
for _, header := range conf.Headers {
req.Header.Set(header.Type, header.Value)
}
return req
}
func performCall(data *url.Values) {
req := makeRequest(data)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("error")
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status, "\n")
fmt.Println("response Headers:", resp.Header, "\n")
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body), "\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment