Ubuntu Pastebin

Paste from sergiusens at Wed, 20 May 2015 19:59:01 +0000

Download as text
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
	}
}
Download as text