header
This commit is contained in:
parent
23bae3f8db
commit
31d6797c4c
43
http.go
43
http.go
|
@ -138,6 +138,8 @@ func WriteHTTPRespAPIOk(w http.ResponseWriter, data any, msg ...any) {
|
|||
if len(msg) > 0 {
|
||||
m = fmt.Sprint(msg...)
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(NewHTTPResp(HTTPRespCodeOKCode, m, data).String()))
|
||||
}
|
||||
|
||||
|
@ -146,6 +148,8 @@ func WriteHTTPRespAPIFailed(w http.ResponseWriter, data any, msg ...any) {
|
|||
if len(msg) > 0 {
|
||||
m = fmt.Sprint(msg...)
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(NewHTTPResp(HTTPRespCodeERCode, m, data).String()))
|
||||
}
|
||||
|
||||
|
@ -154,6 +158,8 @@ func WriteHTTPRespAPIInvalidKey(w http.ResponseWriter, data any, msg ...any) {
|
|||
if len(msg) > 0 {
|
||||
m = fmt.Sprint(msg...)
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(NewHTTPResp(HTTPRespCodeInvalidKeyCode, m, data).String()))
|
||||
}
|
||||
|
||||
|
@ -162,6 +168,8 @@ func WriteHTTPRespAPIInvalidInput(w http.ResponseWriter, data any, msg ...any) {
|
|||
if len(msg) > 0 {
|
||||
m = fmt.Sprint(msg...)
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(NewHTTPResp(HTTPRespCodeInvalidInputCode, m, data).String()))
|
||||
}
|
||||
|
||||
|
@ -170,6 +178,8 @@ func WriteHTTPRespAPIProcessingFailed(w http.ResponseWriter, data any, msg ...an
|
|||
if len(msg) > 0 {
|
||||
m = fmt.Sprint(msg...)
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(NewHTTPResp(HTTPRespCodeProcessingFailedCode, m, data).String()))
|
||||
}
|
||||
|
||||
|
@ -178,11 +188,12 @@ const (
|
|||
HTTPContentTypeUrlencodedUTF8 = "application/x-www-form-urlencoded; charset=utf-8"
|
||||
HTTPContentTypeJson = "application/json"
|
||||
HTTPContentTypeJsonUTF8 = "application/json; charset=UTF-8"
|
||||
// HTTPContentTypePlain = "text/plain; charset=utf-8"
|
||||
HTTPContentTypePlain = "text/plain"
|
||||
HTTPContentTypePlainUTF8 = "text/plain; charset=utf-8"
|
||||
// HTTPContentTypeFormData = "multipart/form-data; charset=utf-8"
|
||||
)
|
||||
|
||||
func HttpGet(u string, args any) []byte {
|
||||
func HttpGet(u string, args any, contentType string, header map[string]string) []byte {
|
||||
arg := NewJSON(args, false).Map()
|
||||
req, err := http.NewRequest("GET", u, nil)
|
||||
if err != nil {
|
||||
|
@ -193,7 +204,13 @@ func HttpGet(u string, args any) []byte {
|
|||
q.Add(k, fmt.Sprint(v))
|
||||
}
|
||||
req.URL.RawQuery = q.Encode()
|
||||
req.Header.Add("Content-Type", HTTPContentTypeJson)
|
||||
if header == nil {
|
||||
header = make(map[string]string)
|
||||
}
|
||||
header["Content-Type"] = contentType
|
||||
for k, v := range header {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
rsp, err := client.Do(req)
|
||||
|
@ -209,7 +226,7 @@ func HttpGet(u string, args any) []byte {
|
|||
return body
|
||||
}
|
||||
|
||||
func HttpPost(contentType string, u string, args any) []byte {
|
||||
func HttpPost(u string, args any, contentType string, header map[string]string) []byte {
|
||||
var req *http.Request
|
||||
var err error
|
||||
|
||||
|
@ -231,7 +248,13 @@ func HttpPost(contentType string, u string, args any) []byte {
|
|||
} else {
|
||||
return nil
|
||||
}
|
||||
req.Header.Add("Content-Type", contentType)
|
||||
if header == nil {
|
||||
header = make(map[string]string)
|
||||
}
|
||||
header["Content-Type"] = contentType
|
||||
for k, v := range header {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
rsp, err := client.Do(req)
|
||||
|
@ -247,7 +270,7 @@ func HttpPost(contentType string, u string, args any) []byte {
|
|||
return body
|
||||
}
|
||||
|
||||
func HttpPostGet(contentType string, u string, argsGET, argsPOST any) []byte {
|
||||
func HttpPostGet(u string, argsGET, argsPOST any, contentType string, header map[string]string) []byte {
|
||||
var req *http.Request
|
||||
var err error
|
||||
|
||||
|
@ -269,7 +292,13 @@ func HttpPostGet(contentType string, u string, argsGET, argsPOST any) []byte {
|
|||
} else {
|
||||
return nil
|
||||
}
|
||||
req.Header.Add("Content-Type", contentType)
|
||||
if header == nil {
|
||||
header = make(map[string]string)
|
||||
}
|
||||
header["Content-Type"] = contentType
|
||||
for k, v := range header {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
|
||||
argGet := NewJSON(argsGET, false).Map()
|
||||
q := req.URL.Query()
|
||||
|
|
10
http_test.go
10
http_test.go
|
@ -117,9 +117,9 @@ func ExampleParseHTTPResp() {
|
|||
}
|
||||
|
||||
func ExampleHttpGet() {
|
||||
i := NewJSONResult(HttpGet("https://jsonplaceholder.typicode.com/posts/1", nil))
|
||||
i1 := NewJSONResult(HttpGet("https://jsonplaceholder.typicode.com/posts", map[string]any{"id": 1}))
|
||||
i2 := NewJSONResult(HttpGet("https://jsonplaceholder.typicode.com/posts", map[string]any{"id": 2}))
|
||||
i := NewJSONResult(HttpGet("https://jsonplaceholder.typicode.com/posts/1", nil, HTTPContentTypePlain, nil))
|
||||
i1 := NewJSONResult(HttpGet("https://jsonplaceholder.typicode.com/posts", map[string]any{"id": 1}, HTTPContentTypePlain, nil))
|
||||
i2 := NewJSONResult(HttpGet("https://jsonplaceholder.typicode.com/posts", map[string]any{"id": 2}, HTTPContentTypePlain, nil))
|
||||
if fmt.Sprint(i.Map()["id"]) != "1" {
|
||||
fmt.Println(i)
|
||||
}
|
||||
|
@ -135,7 +135,7 @@ func ExampleHttpGet() {
|
|||
}
|
||||
|
||||
func ExampleHttpPost() {
|
||||
i1 := NewJSONResult(HttpPost(HTTPContentTypeUrlencoded, "https://jsonplaceholder.typicode.com/posts", map[string]any{"title": "t1", "body": "b1", "userId": 1}))
|
||||
i1 := NewJSONResult(HttpPost("https://jsonplaceholder.typicode.com/posts", map[string]any{"title": "t1", "body": "b1", "userId": 1}, HTTPContentTypeUrlencoded, nil))
|
||||
if fmt.Sprint(i1.Map()["id"]) != "101" {
|
||||
fmt.Println(i1)
|
||||
}
|
||||
|
@ -145,7 +145,7 @@ func ExampleHttpPost() {
|
|||
}
|
||||
|
||||
func ExampleHttpPostGet() {
|
||||
i1 := NewJSONResult(HttpPostGet(HTTPContentTypeUrlencoded, "https://jsonplaceholder.typicode.com/posts", map[string]any{"title": "t2", "body": "b2", "userId": 2}, map[string]any{"title": "t1", "body": "b1", "userId": 1}))
|
||||
i1 := NewJSONResult(HttpPostGet("https://jsonplaceholder.typicode.com/posts", map[string]any{"title": "t2", "body": "b2", "userId": 2}, map[string]any{"title": "t1", "body": "b1", "userId": 1}, HTTPContentTypeUrlencoded, nil))
|
||||
if fmt.Sprint(i1.Map()["id"]) != "101" {
|
||||
fmt.Println(i1)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue