header
This commit is contained in:
parent
fe3f6db5d2
commit
3749401133
|
@ -291,8 +291,14 @@ Content Type
|
|||
- `HTTPContentTypeUrlencodedUTF8`
|
||||
- `HTTPContentTypeJson`
|
||||
- `HTTPContentTypeJsonUTF8`
|
||||
- `HTTPContentTypeXml`
|
||||
- `HTTPContentTypeXmlUTF8`
|
||||
- `HTTPContentTypePlain`
|
||||
- `HTTPContentTypePlainUTF8`
|
||||
- `HTTPContentTypeHtml`
|
||||
- `HTTPContentTypeHtmlUTF8`
|
||||
- `HTTPContentTypeFormData`
|
||||
- `HTTPContentTypeFormDataUTF8`
|
||||
|
||||
方法
|
||||
|
||||
|
|
40
http.go
40
http.go
|
@ -115,6 +115,14 @@ func (r *HTTPRespAPIModel) String() string {
|
|||
return string(data)
|
||||
}
|
||||
|
||||
func (r *HTTPRespAPIModel) Bytes() []byte {
|
||||
data, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return []byte(fmt.Sprintf(`{"code":%d,"msg":"%s"}`, HTTPRespCodeERCode, HTTPRespCodeERMsg))
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
// NewHTTPResp Util Reserved code range (-100,100)
|
||||
func NewHTTPResp(code HTTPRespCode, msg string, data any) *HTTPRespAPIModel {
|
||||
return &HTTPRespAPIModel{
|
||||
|
@ -138,9 +146,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()))
|
||||
w.Header().Set("Content-Type", HTTPContentTypeJson)
|
||||
_, _ = w.Write(NewHTTPResp(HTTPRespCodeOKCode, m, data).Bytes())
|
||||
}
|
||||
|
||||
func WriteHTTPRespAPIFailed(w http.ResponseWriter, data any, msg ...any) {
|
||||
|
@ -148,9 +155,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()))
|
||||
w.Header().Set("Content-Type", HTTPContentTypeJson)
|
||||
_, _ = w.Write(NewHTTPResp(HTTPRespCodeERCode, m, data).Bytes())
|
||||
}
|
||||
|
||||
func WriteHTTPRespAPIInvalidKey(w http.ResponseWriter, data any, msg ...any) {
|
||||
|
@ -158,9 +164,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()))
|
||||
w.Header().Set("Content-Type", HTTPContentTypeJson)
|
||||
_, _ = w.Write(NewHTTPResp(HTTPRespCodeInvalidKeyCode, m, data).Bytes())
|
||||
}
|
||||
|
||||
func WriteHTTPRespAPIInvalidInput(w http.ResponseWriter, data any, msg ...any) {
|
||||
|
@ -168,9 +173,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()))
|
||||
w.Header().Set("Content-Type", HTTPContentTypeJson)
|
||||
_, _ = w.Write(NewHTTPResp(HTTPRespCodeInvalidInputCode, m, data).Bytes())
|
||||
}
|
||||
|
||||
func WriteHTTPRespAPIProcessingFailed(w http.ResponseWriter, data any, msg ...any) {
|
||||
|
@ -178,9 +182,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()))
|
||||
w.Header().Set("Content-Type", HTTPContentTypeJson)
|
||||
_, _ = w.Write(NewHTTPResp(HTTPRespCodeProcessingFailedCode, m, data).Bytes())
|
||||
}
|
||||
|
||||
const (
|
||||
|
@ -188,9 +191,14 @@ const (
|
|||
HTTPContentTypeUrlencodedUTF8 = "application/x-www-form-urlencoded; charset=utf-8"
|
||||
HTTPContentTypeJson = "application/json"
|
||||
HTTPContentTypeJsonUTF8 = "application/json; charset=UTF-8"
|
||||
HTTPContentTypeXml = "application/xml"
|
||||
HTTPContentTypeXmlUTF8 = "application/xml; charset=UTF-8"
|
||||
HTTPContentTypePlain = "text/plain"
|
||||
HTTPContentTypePlainUTF8 = "text/plain; charset=utf-8"
|
||||
// HTTPContentTypeFormData = "multipart/form-data; charset=utf-8"
|
||||
HTTPContentTypeHtml = "text/html"
|
||||
HTTPContentTypeHtmlUTF8 = "text/html; charset=utf-8"
|
||||
HTTPContentTypeFormData = "multipart/form-data"
|
||||
HTTPContentTypeFormDataUTF8 = "multipart/form-data; charset=utf-8"
|
||||
)
|
||||
|
||||
func HttpGet(u string, args any, contentType string, header map[string]string) []byte {
|
||||
|
|
Loading…
Reference in New Issue