Created
November 11, 2019 14:06
-
-
Save tangx/db9c66c9bace38d24d506ced5a5fd4dd to your computer and use it in GitHub Desktop.
发起自定义 http 请求
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
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 ([email protected])") | |
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
如果不设置content-type
, 则无法传递 body 信息