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)
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
In a lot of languages you encode space with something like %20
or +
. I would suggest you to try simple string replacement. For example:
strings.Replace(params, " ", "%20", -1)
You should use url.QueryEscape
.
reqURL := fmt.Sprintf("https://website.com/api?key=%s",
url.QueryEscape("ssh-rsa abcde abc@abc.com"))
// https://website.com/api?key=ssh-rsa+abcde+abc%40abc.com
Also note that you are talking of POST parameters but you are actually setting GET url parameters.