Skip to content

Instantly share code, notes, and snippets.

@tangx
Created November 11, 2019 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tangx/db9c66c9bace38d24d506ced5a5fd4dd to your computer and use it in GitHub Desktop.
Save tangx/db9c66c9bace38d24d506ced5a5fd4dd to your computer and use it in GitHub Desktop.
发起自定义 http 请求
func reqPost(url string, body io.Reader) ([]byte, error) {
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "go-dnspod (shallwedance@126.com)")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return respBody, nil
}
@tangx
Copy link
Author

tangx commented Nov 11, 2019

req.Header.Set("Content-Type", "application/x-www-form-urlencoded") 如果不设置 content-type , 则无法传递 body 信息

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment