add ghttp.Request.GetRawString function

This commit is contained in:
John
2019-02-27 21:17:56 +08:00
parent cdb2cc89c0
commit c2046157d6
4 changed files with 18 additions and 5 deletions

View File

@ -79,7 +79,7 @@ func (r *Request) GetVar(key string, def ... interface{}) gvar.VarRead {
return r.GetRequestVar(key, def...)
}
// 获取原始请求输入字符串
// 获取原始请求输入二进制。
func (r *Request) GetRaw() []byte {
if r.rawContent == nil {
r.rawContent, _ = ioutil.ReadAll(r.Body)
@ -87,6 +87,14 @@ func (r *Request) GetRaw() []byte {
return r.rawContent
}
// 获取原始请求输入字符串。
func (r *Request) GetRawString() string {
if r.rawContent == nil {
r.rawContent, _ = ioutil.ReadAll(r.Body)
}
return string(r.rawContent)
}
// 获取原始json请求输入字符串并解析为json对象
func (r *Request) GetJson() *gjson.Json {
data := r.GetRaw()

View File

@ -16,9 +16,10 @@ func (r *Request) initGet() {
if !r.parsedGet {
r.queryVars = r.URL.Query()
if strings.EqualFold(r.Method, "GET") {
if raw := r.GetRaw(); len(raw) > 0 {
for _, item := range strings.Split(string(raw), "&") {
array := strings.Split(item, "=")
if raw := r.GetRawString(); len(raw) > 0 {
var array []string
for _, item := range strings.Split(raw, "&") {
array = strings.Split(item, "=")
r.queryVars[array[0]] = append(r.queryVars[array[0]], array[1])
}
}

View File

@ -16,7 +16,7 @@ import (
"time"
)
func Test_Params(t *testing.T) {
func Test_Params_Basic(t *testing.T) {
type User struct {
Id int
Name string

View File

@ -9,6 +9,7 @@ package gstr
import "strings"
// Find the position of the first occurrence of a substring in a string.
// It returns -1, if none found.
//
// 返回 needle 在 haystack 中首次出现的数字位置,找不到返回-1。
func Pos(haystack, needle string, startOffset...int) int {
@ -32,6 +33,7 @@ func Pos(haystack, needle string, startOffset...int) int {
}
// Find the position of the first occurrence of a case-insensitive substring in a string.
// It returns -1, if none found.
//
// 返回在字符串 haystack 中 needle 首次出现的数字位置(不区分大小写),找不到返回-1。
func PosI(haystack, needle string, startOffset...int) int {
@ -56,6 +58,7 @@ func PosI(haystack, needle string, startOffset...int) int {
}
// Find the position of the last occurrence of a substring in a string.
// It returns -1, if none found.
//
// 查找指定字符串在目标字符串中最后一次出现的位置,找不到返回-1。
func PosR(haystack, needle string, startOffset...int) int {
@ -81,6 +84,7 @@ func PosR(haystack, needle string, startOffset...int) int {
}
// Find the position of the last occurrence of a case-insensitive substring in a string.
// It returns -1, if none found.
//
// 以不区分大小写的方式查找指定字符串在目标字符串中最后一次出现的位置,找不到返回-1。
func PosRI(haystack, needle string, startOffset...int) int {