doubaoguo7469 2015-05-21 07:10
浏览 309
已采纳

带有空格的POST参数

How do I make a POST request when the post param has spaces in it? Eg. in bash I would do this:

curl -X POST "https://website.com/api" --data-urlencode "key=$SSHKey" <- "ssh-rsa abcde abc@abc.com"

I'm using the http.NewRequest method to send POST requests. http://golang.org/pkg/net/http/#NewRequest

reqURL := fmt.Sprintf("https://website.com/api?key=%s", "ssh-rsa abcde abc@abc.com")

client := &http.Client{}
r, _ := http.NewRequest("POST", reqURL, nil)
client.Do(r)
  • 写回答

3条回答 默认 最新

  • duan20081202 2015-05-21 07:25
    关注

    You could URL encode the parameter using net.url.Values.Encode:

    package main
    
    import "fmt"
    import "net/url"    
    
    func main() {
        values := url.Values{}
        values.Set("key", "foo bar baz")
        fmt.Println(values.Encode())
    }
    

    Output:

    key=foo+bar+baz
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?