~rick-rickspencer3/+junk/go-uploader

« back to all changes in this revision

Viewing changes to main.go

  • Committer: Rick Spencer
  • Date: 2015-06-08 15:15:06 UTC
  • Revision ID: rick.spencer@canonical.com-20150608151506-o6k4wnjqigydakyv
added comments to Go code, deleted unused variable

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
)
14
14
import "gopkg.in/yaml.v2"
15
15
 
 
16
//Define the settings which will be read from the yaml file
16
17
type Config struct {
17
18
    Url string 
18
19
    File string 
19
 
    Params map[string]string 
20
 
    Headers map[string]string 
 
20
    Params map[string]string //any extra params needed for the server
 
21
    Headers map[string]string //any extra headders needed for the server
21
22
}
22
23
 
 
24
//Returns the http.Request configured for the form upload
23
25
func newfileUploadRequest(uri string, params map[string]string, headers map[string]string, path string) (*http.Request, error) {
24
 
  fmt.Println(path)
25
26
  file, err := os.Open(path)
26
27
  if err != nil {
27
28
      return nil, err
62
63
 
63
64
 
64
65
func main() {
65
 
  path, _ := os.Getwd()
66
 
  path += "/test.pdf"
67
 
  
68
 
  
69
 
  
 
66
  //create a Config instance and fill it with the yaml
70
67
  yamlfilename, _ := filepath.Abs("./config.yaml")
71
 
 
72
 
  
73
68
  yamlFile, err := ioutil.ReadFile(yamlfilename)
74
 
  
75
69
  var config Config
76
70
  err = yaml.Unmarshal(yamlFile, &config)
77
71
  
 
72
  //create the http.Request with the configuration
78
73
  request, err := newfileUploadRequest(config.Url, config.Params, config.Headers, config.File)
79
74
  if err != nil {
80
75
      log.Fatal(err)
81
76
  }
 
77
  
 
78
  //make the request
82
79
  client := &http.Client{}
83
80
  resp, err := client.Do(request)
84
81
  if err != nil {
90
87
          log.Fatal(err)
91
88
      }
92
89
    resp.Body.Close()
93
 
      fmt.Println(resp.StatusCode)
94
 
      fmt.Println(resp.Header)
95
 
      fmt.Println(body)
 
90
          
 
91
    //print the results of the request
 
92
    fmt.Println(resp.StatusCode)
 
93
    fmt.Println(resp.Header)
 
94
    fmt.Println(body)
96
95
  }
97
96
}