fix json marshal in http request
This commit is contained in:
parent
12af439a9c
commit
4a85237faf
@ -258,6 +258,12 @@ func (b *BeegoHTTPRequest) AddFilters(fcs ...FilterChain) *BeegoHTTPRequest {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetEscapeHTML is used to set the flag whether escape HTML special characters during processing
|
||||||
|
func (b *BeegoHTTPRequest) SetEscapeHTML(isEscape bool) *BeegoHTTPRequest {
|
||||||
|
b.setting.EscapeHTML = isEscape
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
// Param adds query param in to request.
|
// Param adds query param in to request.
|
||||||
// params build query string as ?key1=value1&key2=value2...
|
// params build query string as ?key1=value1&key2=value2...
|
||||||
func (b *BeegoHTTPRequest) Param(key, value string) *BeegoHTTPRequest {
|
func (b *BeegoHTTPRequest) Param(key, value string) *BeegoHTTPRequest {
|
||||||
@ -334,7 +340,7 @@ func (b *BeegoHTTPRequest) YAMLBody(obj interface{}) (*BeegoHTTPRequest, error)
|
|||||||
// JSONBody adds the request raw body encoded in JSON.
|
// JSONBody adds the request raw body encoded in JSON.
|
||||||
func (b *BeegoHTTPRequest) JSONBody(obj interface{}) (*BeegoHTTPRequest, error) {
|
func (b *BeegoHTTPRequest) JSONBody(obj interface{}) (*BeegoHTTPRequest, error) {
|
||||||
if b.req.Body == nil && obj != nil {
|
if b.req.Body == nil && obj != nil {
|
||||||
byts, err := json.Marshal(obj)
|
byts, err := b.Marshal(obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return b, berror.Wrap(err, InvalidJSONBody, "obj could not be converted to JSON body")
|
return b, berror.Wrap(err, InvalidJSONBody, "obj could not be converted to JSON body")
|
||||||
}
|
}
|
||||||
@ -345,6 +351,17 @@ func (b *BeegoHTTPRequest) JSONBody(obj interface{}) (*BeegoHTTPRequest, error)
|
|||||||
return b, nil
|
return b, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *BeegoHTTPRequest) Marshal(obj interface{}) ([]byte, error) {
|
||||||
|
bf := bytes.NewBuffer([]byte{})
|
||||||
|
jsonEncoder := json.NewEncoder(bf)
|
||||||
|
jsonEncoder.SetEscapeHTML(b.setting.EscapeHTML)
|
||||||
|
err := jsonEncoder.Encode(obj)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return bf.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (b *BeegoHTTPRequest) buildURL(paramBody string) {
|
func (b *BeegoHTTPRequest) buildURL(paramBody string) {
|
||||||
// build GET url with query string
|
// build GET url with query string
|
||||||
if b.req.Method == "GET" && len(paramBody) > 0 {
|
if b.req.Method == "GET" && len(paramBody) > 0 {
|
||||||
|
|||||||
@ -18,6 +18,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -435,3 +436,13 @@ func TestBeegoHTTPRequestXMLBody(t *testing.T) {
|
|||||||
// TODO
|
// TODO
|
||||||
func TestBeegoHTTPRequestResponseForValue(t *testing.T) {
|
func TestBeegoHTTPRequestResponseForValue(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBeegoHTTPRequestMarshal(t *testing.T) {
|
||||||
|
req := Post("http://beego.me")
|
||||||
|
req.SetEscapeHTML(false)
|
||||||
|
body := map[string]interface{} {
|
||||||
|
"escape": "left&right",
|
||||||
|
}
|
||||||
|
b, _ := req.Marshal(body)
|
||||||
|
assert.Equal(t,fmt.Sprintf(`{"escape":"left&right"}%s`, "\n"), string(b))
|
||||||
|
}
|
||||||
|
|||||||
@ -37,6 +37,7 @@ type BeegoHTTPSettings struct {
|
|||||||
Retries int // if set to -1 means will retry forever
|
Retries int // if set to -1 means will retry forever
|
||||||
RetryDelay time.Duration
|
RetryDelay time.Duration
|
||||||
FilterChains []FilterChain
|
FilterChains []FilterChain
|
||||||
|
EscapeHTML bool // if set to false means will not escape escape HTML special characters during processing, default true
|
||||||
}
|
}
|
||||||
|
|
||||||
// createDefaultCookie creates a global cookiejar to store cookies.
|
// createDefaultCookie creates a global cookiejar to store cookies.
|
||||||
@ -66,6 +67,7 @@ var defaultSetting = BeegoHTTPSettings{
|
|||||||
ReadWriteTimeout: 60 * time.Second,
|
ReadWriteTimeout: 60 * time.Second,
|
||||||
Gzip: true,
|
Gzip: true,
|
||||||
FilterChains: make([]FilterChain, 0, 4),
|
FilterChains: make([]FilterChain, 0, 4),
|
||||||
|
EscapeHTML: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user