在 GoFrame 框架中可以使用 r.Request.Get()
接收参数,但application/json
参数不能以这种方式接收。
可以使用 r.Request.GetRaw()
接收,但返回值是[]byte
类型需要进一步转化很不方便。
于是我便想写一个公共处理让application/json
参数可以像接收form-data
参数一样接收。
在util包下写一个公共方法 request.go
package util
import (
"encoding/json"
"github.com/gogf/gf/g/net/ghttp"
"github.com/gogf/gf/g/util/gconv"
"io/ioutil"
"strings"
)
// 设置请求
func Request(r *ghttp.Request) {
contentType := r.Request.Header.Get("Content-type")
if strings.Index(contentType, "application/json") > -1 {
body,_ := ioutil.ReadAll(r.Body)
var queryMap map[string]interface{}
err := json.Unmarshal(body, &queryMap)
if err == nil {
method := r.Request.Method
var value string
for key := range queryMap {