commands/http: Use net/url querystring encoder
Matt Bell committed
Nov 3, 2014 at 23:58 UTC
e57cd9b8573ad0e7ffd49eefbf41b04774cfa5dc
1 file changed
+9
-8
commands/http/client.go
+9
-8
@@ -6,13 +6,14 @@ import (
6
"fmt"
7
"io"
8
"net/http"
9
+ "net/url"
10
"strings"
11
12
cmds "github.com/jbenet/go-ipfs/commands"
13
)
14
15
const (
15
- ApiUrlFormat = "http://%s%s/%s"
16
+ ApiUrlFormat = "http://%s%s/%s?%s"
17
ApiPath = "/api/v0" // TODO: make configurable
18
)
19
@@ -30,9 +31,6 @@ func NewClient(address string) Client {
31
}
32
33
func (c *client) Send(req cmds.Request) (cmds.Response, error) {
33
- path := strings.Join(req.Path(), "/")
34
- url := fmt.Sprintf(ApiUrlFormat, c.serverAddress, ApiPath, path)
35
-
34
var userEncoding string
35
if enc, found := req.Option(cmds.EncShort); found {
36
userEncoding = enc.(string)
@@ -46,9 +44,9 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
44
// TODO: handle multiple files with multipart
45
var in io.Reader
46
49
- query := "?"
47
+ query := url.Values{}
48
for k, v := range req.Options() {
51
- query += "&" + k + "=" + v.(string)
49
+ query.Set(k, v.(string))
50
}
51
52
args := req.Arguments()
@@ -61,7 +59,7 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
59
}
60
61
if argDef.Type == cmds.ArgString {
64
- query += "&arg=" + arg.(string)
62
+ query.Add("arg", arg.(string))
63
64
} else {
65
// TODO: multipart
@@ -72,7 +70,10 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
70
}
71
}
72
75
- httpRes, err := http.Post(url+query, "application/octet-stream", in)
73
+ path := strings.Join(req.Path(), "/")
74
+ url := fmt.Sprintf(ApiUrlFormat, c.serverAddress, ApiPath, path, query.Encode())
75
+
76
+ httpRes, err := http.Post(url, "application/octet-stream", in)
77
if err != nil {
78
return nil, err
79
}