Skip to content

Instantly share code, notes, and snippets.

@zz
Created March 13, 2018 16:02
Show Gist options
  • Save zz/e8633d9a6d44127c75d5420931a0a3a8 to your computer and use it in GitHub Desktop.
Save zz/e8633d9a6d44127c75d5420931a0a3a8 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