package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
const (
timeoutMinutes = 1
apiUrl = "http://api.com"
filePath = "the_file_to_open"
)
func loop() {
r, err := os.Open(filePath)
if err != nil {
log.Println("Cannot open file:", err)
return
}
if err := post(r); err != nil {
log.Println("Issue while posting:", err)
return
}
}
func post(r io.Reader) error {
req, err := http.NewRequest("POST", apiUrl, r)
if err != nil {
return err
}
req.Header.Set("Content-Type", "image/png")
//req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// Decode response into a
// j := json.NewDecoder(resp.Body)
// err := j.Decode(&myResponseStructType)
responseBodyRaw, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
fmt.Println("Response Body:", string(responseBodyRaw))
return nil
}
func main() {
timeout := time.NewTimer(timeoutMinutes * time.Minute)
for {
loop()
timeout.Reset(timeoutMinutes * time.Minute)
<-timeout.C
}
}