forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
353 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package helper | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
|
||
jsoniter "github.com/json-iterator/go" | ||
) | ||
|
||
// Response 请求响应结果 | ||
type Response struct { | ||
Status int `json:"status"` | ||
Body string `json:"body"` | ||
Data interface{} `json:"data"` | ||
Headers map[string]interface{} `json:"headers"` | ||
} | ||
|
||
// RequestGet 发送GET请求 | ||
func RequestGet(url string, params map[string]interface{}, headers map[string]string) Response { | ||
return RequestSend("GET", url, params, nil, headers) | ||
} | ||
|
||
// RequestPost 发送POST请求 | ||
func RequestPost(url string, data interface{}, headers map[string]string) Response { | ||
return RequestSend("POST", url, map[string]interface{}{}, data, headers) | ||
} | ||
|
||
// RequestSend 发送Request请求 | ||
func RequestSend(method string, url string, params map[string]interface{}, data interface{}, headers map[string]string) Response { | ||
|
||
var body []byte | ||
var err error | ||
if data != nil { | ||
body, err = jsoniter.Marshal(data) | ||
if err != nil { | ||
return Response{ | ||
Status: 500, | ||
Body: err.Error(), | ||
Data: map[string]interface{}{"code": 500, "message": err.Error()}, | ||
Headers: map[string]interface{}{ | ||
"Content-Type": "application/json;charset=utf8", | ||
}, | ||
} | ||
} | ||
} | ||
|
||
req, err := http.NewRequest(method, url, bytes.NewBuffer(body)) | ||
if err != nil { | ||
return Response{ | ||
Status: 500, | ||
Body: err.Error(), | ||
Data: map[string]interface{}{"code": 500, "message": err.Error()}, | ||
Headers: map[string]interface{}{ | ||
"Content-Type": "application/json;charset=utf8", | ||
}, | ||
} | ||
} | ||
|
||
// Request Header | ||
for name, header := range headers { | ||
req.Header.Set(name, header) | ||
} | ||
|
||
resp, err := (&http.Client{}).Do(req) | ||
if err != nil { | ||
return Response{ | ||
Status: 0, | ||
Body: err.Error(), | ||
Data: map[string]interface{}{"code": 500, "message": err.Error()}, | ||
Headers: map[string]interface{}{ | ||
"Content-Type": "application/json;charset=utf8", | ||
}, | ||
} | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err = ioutil.ReadAll(resp.Body) // response body is []byte | ||
if err != nil { | ||
return Response{ | ||
Status: 500, | ||
Body: err.Error(), | ||
Data: map[string]interface{}{"code": resp.StatusCode, "message": err.Error()}, | ||
Headers: map[string]interface{}{ | ||
"Content-Type": "application/json;charset=utf8", | ||
}, | ||
} | ||
} | ||
|
||
// JSON 解析 | ||
var res interface{} | ||
if strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") { | ||
err = jsoniter.Unmarshal(body, &res) | ||
if err != nil { | ||
return Response{ | ||
Status: 500, | ||
Body: err.Error(), | ||
Data: map[string]interface{}{"code": resp.StatusCode, "message": err.Error()}, | ||
Headers: map[string]interface{}{ | ||
"Content-Type": "application/json;charset=utf8", | ||
}, | ||
} | ||
} | ||
} | ||
respHeaders := map[string]interface{}{} | ||
for name := range resp.Header { | ||
respHeaders[name] = resp.Header.Get(name) | ||
} | ||
return Response{ | ||
Status: resp.StatusCode, | ||
Body: string(body), | ||
Data: res, | ||
Headers: respHeaders, | ||
} | ||
} |
Oops, something went wrong.