aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Gerrand <adg@golang.org>2010-09-02 10:01:34 +1000
committerAndrew Gerrand <adg@golang.org>2010-09-02 10:01:34 +1000
commit2fe0f8da52d01bb2bf705a7b0f4fec933b65c6ea (patch)
tree5615863d1e91c4de35ab82d5d5cb96070fd6dfaa
parentf8a67d79d19776efd43dc36d597bb3cbfdb779f3 (diff)
downloadgo-2fe0f8da52d01bb2bf705a7b0f4fec933b65c6ea.tar.gz
go-2fe0f8da52d01bb2bf705a7b0f4fec933b65c6ea.zip
http: add PostForm function to post url-encoded key/value data.
This is a common task, so it makes sense to have a helper to do it. (App Engine doesn't like "Transfer-Encoding: chunked" for POST bodies, which is the default for regular Post.) R=rsc CC=golang-dev https://golang.org/cl/2113041
-rw-r--r--src/pkg/http/client.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/pkg/http/client.go b/src/pkg/http/client.go
index 50b6e530d9..d77bf0e759 100644
--- a/src/pkg/http/client.go
+++ b/src/pkg/http/client.go
@@ -8,12 +8,14 @@ package http
import (
"bufio"
+ "bytes"
"crypto/tls"
"encoding/base64"
"fmt"
"io"
"net"
"os"
+ "strconv"
"strings"
)
@@ -161,6 +163,48 @@ func Post(url string, bodyType string, body io.Reader) (r *Response, err os.Erro
return send(&req)
}
+// PostForm issues a POST to the specified URL,
+// with data's keys and values urlencoded as the request body.
+//
+// Caller should close r.Body when done reading it.
+func PostForm(url string, data map[string]string) (r *Response, err os.Error) {
+ var req Request
+ req.Method = "POST"
+ req.ProtoMajor = 1
+ req.ProtoMinor = 1
+ req.Close = true
+ body := urlencode(data)
+ req.Body = nopCloser{body}
+ req.Header = map[string]string{
+ "Content-Type": "application/x-www-form-urlencoded",
+ "Content-Length": strconv.Itoa(body.Len()),
+ }
+ req.ContentLength = int64(body.Len())
+
+ req.URL, err = ParseURL(url)
+ if err != nil {
+ return nil, err
+ }
+
+ return send(&req)
+}
+
+func urlencode(data map[string]string) (b *bytes.Buffer) {
+ b = new(bytes.Buffer)
+ first := true
+ for k, v := range data {
+ if first {
+ first = false
+ } else {
+ b.WriteByte('&')
+ }
+ b.WriteString(URLEscape(k))
+ b.WriteByte('=')
+ b.WriteString(URLEscape(v))
+ }
+ return
+}
+
// Head issues a HEAD to the specified URL.
func Head(url string) (r *Response, err os.Error) {
var req Request