-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.go
49 lines (45 loc) · 1.15 KB
/
web.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
49
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
)
func postFormWithAuth(url string, data url.Values, key string) (r JSONResponse, err error) {
resp, err := postWithAuth(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()), key)
if err != nil {
return r, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return r, err
}
err = json.Unmarshal(body, &r)
if err != nil {
return r, fmt.Errorf("POST %v %v", strconv.Quote(url), string(body))
}
return r, nil
}
func postWithAuth(url, contentType string, body io.Reader, key string) (resp *http.Response, err error) {
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
req.Header.Set("Authorization", key)
return http.DefaultClient.Do(req)
}
func getWithAuth(url string, key string) (resp *http.Response, err error) {
// url := remoteURL + pathname
req, err := http.NewRequest("GET", url, nil)
req.Header.Set("Authorization", key)
if err != nil {
return nil, err
}
return http.DefaultClient.Do(req)
}