Created
July 6, 2016 05:53
-
-
Save josephspurrier/fd8e62dfac330c2964116046e46cd959 to your computer and use it in GitHub Desktop.
Test POST Request in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net/http" | |
"net/http/httptest" | |
"net/url" | |
"strings" | |
) | |
func main() { | |
r := http.DefaultServeMux | |
r.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) { | |
r.ParseForm() | |
log.Println("Name:", r.FormValue("name")) | |
}) | |
form := url.Values{} | |
form.Add("name", "joe") | |
req, _ := http.NewRequest("POST", "/users", strings.NewReader(form.Encode())) | |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | |
r.ServeHTTP(httptest.NewRecorder(), req) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment