-
Notifications
You must be signed in to change notification settings - Fork 2
/
post.go
48 lines (45 loc) · 1.18 KB
/
post.go
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
// Copyright 2017 Matt Spaulding. All rights reserved.
// Package wreck implements the wreck command line tool
package wreck
import (
"bytes"
"io"
"io/ioutil"
"net/http"
)
// Post will perform a POST request
func Post(url string) (string, error) {
client := http.Client{}
var contentReader io.Reader
content := UserConfig.GetString("content")
if content != "" {
contentReader = bytes.NewReader([]byte(content))
}
req, err := http.NewRequest("POST", url, contentReader)
if err != nil {
return "", err
}
username := UserConfig.GetString("username")
password := UserConfig.GetString("password")
if username != "" && password != "" {
req.SetBasicAuth(username, password)
}
commonHeaders := GlobalConfig.GetStringMapString("headers.common")
for k, v := range commonHeaders {
req.Header.Add(http.CanonicalHeaderKey(k), v)
}
postHeaders := GlobalConfig.GetStringMapString("headers.post")
for k, v := range postHeaders {
req.Header.Add(http.CanonicalHeaderKey(k), v)
}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
respContent, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(respContent), nil
}