Skip to content

Instantly share code, notes, and snippets.

@aamsur
Created March 13, 2017 02:08
Show Gist options
  • Save aamsur/2c6f78c559071071990e7103f8cd8282 to your computer and use it in GitHub Desktop.
Save aamsur/2c6f78c559071071990e7103f8cd8282 to your computer and use it in GitHub Desktop.
golang http post request
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
func main() {
request_url := "http://localhost/index.php"
// 要 POST的 参数
form := url.Values{
"username": {"xiaoming"},
"address": {"beijing"},
"subject": {"Hello"},
"from": {"china"},
}
// func Post(url string, bodyType string, body io.Reader) (resp *Response, err error) {
//对form进行编码
body := bytes.NewBufferString(form.Encode())
rsp, err := http.Post(request_url, "application/x-www-form-urlencoded", body)
if err != nil {
panic(err)
}
defer rsp.Body.Close()
body_byte, err := ioutil.ReadAll(rsp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(body_byte))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment