Skip to content

Commit 99fc304

Browse files
Nishant Singhalfacebook-github-bot
Nishant Singhal
authored andcommitted
Properly encode the query parameters
Summary: The fix here is to replace `PathEscape` with `QueryEscape` to properly encode the query portion of the URL. Reviewed By: patapizza, ChrisyShine Differential Revision: D39864678 fbshipit-source-id: 330cc226adc1fcf8116da46e553c16dd599bfe5a
1 parent c22c59c commit 99fc304

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

message.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (c *Client) Speech(req *MessageRequest) (*MessageResponse, error) {
121121
}
122122

123123
func buildParseQuery(req *MessageRequest) string {
124-
q := fmt.Sprintf("?q=%s", url.PathEscape(req.Query))
124+
q := fmt.Sprintf("?q=%s", url.QueryEscape(req.Query))
125125
if req.N != 0 {
126126
q += fmt.Sprintf("&n=%d", req.N)
127127
}
@@ -131,7 +131,7 @@ func buildParseQuery(req *MessageRequest) string {
131131
if req.Context != nil {
132132
b, _ := json.Marshal(req.Context)
133133
if b != nil {
134-
q += fmt.Sprintf("&context=%s", url.PathEscape(string(b)))
134+
q += fmt.Sprintf("&context=%s", url.QueryEscape(string(b)))
135135
}
136136
}
137137

message_test.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"bytes"
77
"net/http"
88
"net/http/httptest"
9-
"net/url"
109
"reflect"
1110
"testing"
1211
)
@@ -149,18 +148,18 @@ func TestSpeech(t *testing.T) {
149148
}
150149

151150
func Test_buildParseQuery(t *testing.T) {
152-
want := "?q=" + url.PathEscape("hello world") +
151+
want := "?q=" + "hello+world%26foo" +
153152
"&n=1&tag=tag" +
154153
"&context=" +
155-
url.PathEscape("{"+
156-
"\"reference_time\":\"2014-10-30T12:18:45-07:00\","+
157-
"\"timezone\":\"America/Los_Angeles\","+
158-
"\"locale\":\"en_US\","+
159-
"\"coords\":{\"lat\":32.47104,\"long\":-122.14703}"+
160-
"}")
154+
"%7B" +
155+
"%22reference_time%22%3A%222014-10-30T12%3A18%3A45-07%3A00%22%2C" +
156+
"%22timezone%22%3A%22America%2FLos_Angeles%22%2C" +
157+
"%22locale%22%3A%22en_US%22%2C" +
158+
"%22coords%22%3A%7B%22lat%22%3A32.47104%2C%22long%22%3A-122.14703%7D" +
159+
"%7D"
161160

162161
got := buildParseQuery(&MessageRequest{
163-
Query: "hello world",
162+
Query: "hello world&foo",
164163
N: 1,
165164
Tag: "tag",
166165
Context: &MessageContext{

0 commit comments

Comments
 (0)