Skip to content

Commit b452c0f

Browse files
committed
HTTP requests with header
1 parent 9f18eff commit b452c0f

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

httpHeader/main.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"net/http"
8+
"os"
9+
)
10+
11+
func main() {
12+
req, err := http.NewRequest(http.MethodGet, "https://api.github.com/users/bsnux", nil)
13+
if err != nil {
14+
fmt.Printf("client: could not create request: %s\n", err)
15+
os.Exit(1)
16+
}
17+
18+
req.Header.Set("Accept", "application/vnd.github.v3+json")
19+
client := &http.Client{}
20+
res, err := client.Do(req)
21+
if err != nil {
22+
fmt.Printf("client: error sending http request: %s\n", err)
23+
os.Exit(1)
24+
}
25+
26+
body, err := ioutil.ReadAll(res.Body)
27+
if err != nil {
28+
fmt.Printf("client: could not read response body: %s\n", err)
29+
os.Exit(1)
30+
}
31+
32+
var result map[string]interface{}
33+
jsonErr := json.Unmarshal(body, &result)
34+
if jsonErr != nil {
35+
fmt.Printf("json: could not parse json: %s\n", err)
36+
os.Exit(1)
37+
}
38+
fmt.Println(result["bio"])
39+
}

0 commit comments

Comments
 (0)