Created
May 5, 2018 06:36
-
-
Save dwdraju/00f0b08804b9f40cb749895c61aa27b5 to your computer and use it in GitHub Desktop.
golang http post request
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 ( | |
"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