Created
May 26, 2015 11:36
-
-
Save pm-kartik-sura/4bbbd70dec915519084c to your computer and use it in GitHub Desktop.
HTTP 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
| // HTTPRequester project main.go | |
| package main | |
| import ( | |
| "fmt" | |
| "io/ioutil" | |
| "log" | |
| "net/http" | |
| "time" | |
| ) | |
| func CallHTTP(url string) error { | |
| req, err := http.NewRequest("GET", url, nil) | |
| // req.Header.Add("User-Agent", r.UserAgent()) | |
| req.ParseForm() | |
| timeout := time.Duration(time.Duration(1000) * time.Millisecond) | |
| client := http.Client{ | |
| Timeout: timeout, | |
| } | |
| response, err := client.Do(req) | |
| if err != nil { | |
| log.Println(err) | |
| return err | |
| } | |
| defer response.Body.Close() | |
| contents, err := ioutil.ReadAll(response.Body) | |
| if err != nil { | |
| log.Println(err) | |
| return err | |
| } | |
| fmt.Println("response contents: ", string(contents)) | |
| return nil | |
| } | |
| func main() { | |
| CallHTTP("http://www.google.com") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment