-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
43 lines (35 loc) · 1.29 KB
/
api.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
package requests
import (
"context"
"io"
"net/http"
"net/url"
"strings"
)
var s = New()
// Get send get request
func Get(url string) (*http.Response, error) {
return s.Do(context.Background(), MethodGet, URL(url))
}
// Post send post request
func Post(url string, contentType string, body io.Reader) (*http.Response, error) {
return s.Do(context.TODO(), MethodPost, URL(url), Header("Content-Type", contentType), Body(body))
}
// PUT send put request
func PUT(url, contentType string, body io.Reader) (*http.Response, error) {
return s.Do(context.TODO(), Method("PUT"), URL(url), Header("Content-Type", contentType), Body(body))
}
// Delete send delete request
func Delete(url, contentType string, body io.Reader) (*http.Response, error) {
return s.Do(context.TODO(), Method("DELETE"), URL(url), Header("Content-Type", contentType), Body(body))
}
// Head send post request
func Head(url string) (resp *http.Response, err error) {
return s.Do(context.Background(), Method("HEAD"), URL(url))
}
// PostForm send post request, content-type = application/x-www-form-urlencoded
func PostForm(url string, data url.Values) (*http.Response, error) {
return s.Do(context.TODO(), MethodPost, URL(url), Header("Content-Type", "application/x-www-form-urlencoded"),
Body(strings.NewReader(data.Encode())),
)
}