mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
39 lines
586 B
Go
39 lines
586 B
Go
package appsrv
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"net/http"
|
|
|
|
"yunion.io/x/jsonutils"
|
|
)
|
|
|
|
func Fetch(req *http.Request) ([]byte, error) {
|
|
defer req.Body.Close()
|
|
return ioutil.ReadAll(req.Body)
|
|
}
|
|
|
|
func FetchStruct(req *http.Request, v interface{}) error {
|
|
b, e := Fetch(req)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if len(b) > 0 {
|
|
return json.Unmarshal(b, v)
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func FetchJSON(req *http.Request) (jsonutils.JSONObject, error) {
|
|
b, e := Fetch(req)
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
if len(b) > 0 {
|
|
return jsonutils.Parse(b)
|
|
} else {
|
|
return nil, nil
|
|
}
|
|
}
|