-
Notifications
You must be signed in to change notification settings - Fork 6
/
http.go
37 lines (32 loc) · 807 Bytes
/
http.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
package ups
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strconv"
)
func HttpPostRequest(url, msgbody string) (string, error) {
client := &http.Client{}
body := bytes.NewBufferString(msgbody)
clength := strconv.Itoa(len(msgbody))
r, _ := http.NewRequest("POST", url, body)
r.Header.Add("User-Agent", "NicerWatch")
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r.Header.Add("Content-Length", clength)
rsp, err := client.Do(r)
if err != nil {
return "", err
}
defer func() {
rsp.Body.Close()
}()
if rsp.StatusCode == 200 {
bodyBytes, err := ioutil.ReadAll(rsp.Body)
return fmt.Sprintf("%v", bodyBytes), err
} else if err != nil {
return "", err
} else {
return "", fmt.Errorf("The remote end did not return a HTTP 200 (OK) response:%#v\n", rsp)
}
}