package main | |
import ( | |
"fmt" | |
"http" | |
"io/ioutil" | |
"os" | |
) | |
func main() { | |
response, _, err := http.Get("http://golang.org/") | |
if err != nil { | |
fmt.Printf("%s", err) | |
os.Exit(1) | |
} else { | |
defer response.Body.Close() | |
contents, err := ioutil.ReadAll(response.Body) | |
if err != nil { | |
fmt.Printf("%s", err) | |
os.Exit(1) | |
} | |
fmt.Printf("%s\n", string(contents)) | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
Thanks for the tip, @case. I got this running but had to import |
This comment has been minimized.
This comment has been minimized.
golang 1.2.1. import ("net/http") |
This comment has been minimized.
This comment has been minimized.
Thanks for sharing! |
This comment has been minimized.
This comment has been minimized.
Thanks! This helped me! |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
Got the below error when tried it in playground
|
This comment has been minimized.
This comment has been minimized.
You replace the printf/exit combination with: Log.Fatal; |
This comment has been minimized.
This comment has been minimized.
problem in line 11 |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
fmt.Printf("%s", err) --> fmt.Printf("%s\n", err) |
This comment has been minimized.
This comment has been minimized.
thanks for the example, i was looking for this. |
This comment has been minimized.
This comment has been minimized.
@daft117 actually it is, because it prevents you from making terrible mistakes coming from sloppiness ;) |
This comment has been minimized.
This comment has been minimized.
Thanks for sharing, has come in useful! |
This comment has been minimized.
This comment has been minimized.
Very useful ! Thank you ! |
This comment has been minimized.
This comment has been minimized.
Hey thanks for this. I noticed that if you simulate a situation where you cannot resolve the adress you are trying to 'get', the error handling on handling on line 13 isn't that useful. This is some example output. Using |
This comment has been minimized.
This comment has been minimized.
I wanted to know if there is any other way to read from response body apart from what has been mentioned. I'm new to Golang and just exploring other means and ways to do things. |
This comment has been minimized.
This comment has been minimized.
Hi, So lets say I wanted to implement this as a rest endpoint. And in the method it would look up in a cache to see if the response was already cached. If cached, return cache, if not, do a new get and save that in the cache. How would a possible solution for this look like? |
This comment has been minimized.
This comment has been minimized.
I can't seem to get this to work with a url with https. Is there additional setup needed? |
This comment has been minimized.
This comment has been minimized.
Hello! |
This comment has been minimized.
This comment has been minimized.
Hi all, sorry I didn't update this for so long. I forgot about it and didn't see any notifications for your posts. Anyway, it's updated now. Enjoy! |
This comment has been minimized.
This comment has been minimized.
thanks, quickly got me running with go/http.. |
This comment has been minimized.
This comment has been minimized.
Great! Exactly the kind of bare bones example I needed to see. Thanks for sharing this. |
This comment has been minimized.
This comment has been minimized.
Thanks for sharing, it's so great. |
This comment has been minimized.
This comment has been minimized.
This info was very helpful, however. Is there a way to pick out just the response status? |
This comment has been minimized.
This comment has been minimized.
The response status (200, 404, etc.) is available in |
This comment has been minimized.
This comment has been minimized.
Hi All, Pasted my code below: ) func main() {
Please help me on this to fix the issue. |
This comment has been minimized.
This comment has been minimized.
I tried in below way and got output:package main ) func main() { /* body, err := ioutil.ReadAll(res.Body) // err := json.Unmarshal(body , &p) } After execution:go run Sample_restcall.go: response from GET request &{200 OK 200 HTTP/1.1 1 1 map[Access-Control-Allow-Origin:[*] Content-Type:[application/json; charset=utf-8] Set-Cookie:[dcbfef97e14c2a3cdf00a5 I could see response status code as 200 , how to see response content and how to decode to normal json format. |
This comment has been minimized.
This comment has been minimized.
Naveennani777, you see what it is, slice of bytes, you have to convert it to string, try this:
|
This comment has been minimized.
This comment has been minimized.
Thanks alot sibyakin , Its working now. |
This comment has been minimized.
This comment has been minimized.
thank you :) |
This comment has been minimized.
This comment has been minimized.
Thank you :) |
This comment has been minimized.
This comment has been minimized.
How to pass header in GET request |
This comment has been minimized.
This comment has been minimized.
How to pass header in GET request |
This comment has been minimized.
This comment has been minimized.
Here is an example: client := &http.Client{}
apiURL := "https://myapi.url/data"
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
return nil, err
}
// add header to request
req.Header.Add("authkey", "...")
// perform get request
res, err := client.Do(req) |
This comment has been minimized.
This comment has been minimized.
You dont need the |
This comment has been minimized.
This comment has been minimized.
@R4wm, done. |
This comment has been minimized.
Thanks for sharing this — I think
http
needs to behet/http
, and this line:response, _, err := http.Get("http://golang.org/")
works if you remove the_
:response, err := http.Get("http://golang.org/")