1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-06-12 22:07:47 +02:00

api/handler/rpc: process all methods and merge url params to json body (#1427)

* api/handler/rpc: process all methods and merge url params to json body

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>

* add merge json test

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Vasiliy Tolstov
2020-03-27 10:59:31 +03:00
committed by GitHub
parent 1a53307a78
commit b38da6ced0
4 changed files with 41 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import (
"strconv"
"strings"
jsonpatch "github.com/evanphx/json-patch/v5"
"github.com/joncalhoun/qson"
"github.com/micro/go-micro/v2/api"
"github.com/micro/go-micro/v2/api/handler"
@ -273,13 +274,32 @@ func requestPayload(r *http.Request) ([]byte, error) {
if len(r.URL.RawQuery) > 0 {
return qson.ToJSON(r.URL.RawQuery)
}
case "PATCH", "POST":
case "PATCH", "POST", "PUT", "DELETE":
urlParams := []byte("{}")
bodyParams := []byte("{}")
var err error
if len(r.URL.RawQuery) > 0 {
if urlParams, err = qson.ToJSON(r.URL.RawQuery); err != nil {
return nil, err
}
}
buf := bufferPool.Get()
defer bufferPool.Put(buf)
if _, err := buf.ReadFrom(r.Body); err != nil {
return nil, err
}
if b := buf.Bytes(); len(b) > 0 {
bodyParams = b
}
if out, err := jsonpatch.MergeMergePatches(urlParams, bodyParams); err == nil {
return out, nil
}
//fallback to previous unknown behaviour
return buf.Bytes(), nil
}
return []byte{}, nil