Skip to content

Commit

Permalink
Merge pull request #133 from mark4z/fix_post_method
Browse files Browse the repository at this point in the history
encode params before post method
  • Loading branch information
binbin0325 authored Oct 15, 2020
2 parents 6c8cd18 + e30bf7e commit 28f2dda
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
20 changes: 20 additions & 0 deletions clients/config_client/config_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,3 +648,23 @@ func TestCancelListenConfig(t *testing.T) {
assert.Equal(t, localConfigTest.Content, context)
})
}

func TestGetConfigWithSpecialSymbol(t *testing.T) {
contentStr := "hello world!!@#$%^&&*()"

client := cretateConfigClientTest()
success, err := client.PublishConfig(vo.ConfigParam{
DataId: "special_symbol",
Group: localConfigTest.Group,
Content: contentStr})

assert.Nil(t, err)
assert.True(t, success)

content, err := client.GetConfig(vo.ConfigParam{
DataId: "special_symbol",
Group: localConfigTest.Group})

assert.Nil(t, err)
assert.Equal(t, contentStr, content)
}
13 changes: 4 additions & 9 deletions common/http_agent/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,15 @@ import (
"net/http"
"strings"
"time"

"github.com/nacos-group/nacos-sdk-go/util"
)

func post(path string, header http.Header, timeoutMs uint64, params map[string]string) (response *http.Response, err error) {
client := http.Client{}
client.Timeout = time.Millisecond * time.Duration(timeoutMs)
var body string
for key, value := range params {
if len(value) > 0 {
body += key + "=" + value + "&"
}
}
if strings.HasSuffix(body, "&") {
body = body[:len(body)-1]
}

body := util.GetUrlFormedMap(params)
request, errNew := http.NewRequest(http.MethodPost, path, strings.NewReader(body))
if errNew != nil {
err = errNew
Expand Down
10 changes: 10 additions & 0 deletions util/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package util
import (
"encoding/json"
"net"
"net/url"
"strconv"
"time"

Expand Down Expand Up @@ -98,3 +99,12 @@ func GetDurationWithDefault(metadata map[string]string, key string, defaultDurat
}
return defaultDuration
}

func GetUrlFormedMap(source map[string]string) (urlEncoded string) {
urlEncoder := url.Values{}
for key, value := range source {
urlEncoder.Add(key, value)
}
urlEncoded = urlEncoder.Encode()
return
}

0 comments on commit 28f2dda

Please sign in to comment.