Kohei
Posted on September 4, 2018
This article was originally published on GitHub.
nat/http
package
Create a request
$ touch request.go
// request.go
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
res, err := http.Get("https://api.github.com/users/defunkt")
if err != nil {
panic(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
}
$ go run request.go | jq
{
"login": "defunkt",
"id": 2,
"node_id": "MDQ6VXNlcjI=",
"avatar_url": "https://avatars0.githubusercontent.com/u/2?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/defunkt",
"html_url": "https://github.com/defunkt",
"followers_url": "https://api.github.com/users/defunkt/followers",
"following_url": "https://api.github.com/users/defunkt/following{/other_user}",
"gists_url": "https://api.github.com/users/defunkt/gists{/gist_id}",
"starred_url": "https://api.github.com/users/defunkt/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/defunkt/subscriptions",
"organizations_url": "https://api.github.com/users/defunkt/orgs",
"repos_url": "https://api.github.com/users/defunkt/repos",
"events_url": "https://api.github.com/users/defunkt/events{/privacy}",
"received_events_url": "https://api.github.com/users/defunkt/received_events",
"type": "User",
"site_admin": true,
"name": "Chris Wanstrath",
"company": "@github ",
"blog": "http://chriswanstrath.com/",
"location": "San Francisco",
"email": null,
"hireable": true,
"bio": "🍔 ",
"public_repos": 107,
"public_gists": 273,
"followers": 20213,
"following": 210,
"created_at": "2007-10-20T05:24:19Z",
"updated_at": "2018-06-05T19:29:51Z"
}
Setup a server
$ touch server.go
// server.go
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", r.URL.Path)
})
fmt.Println("http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
$ go run server.go
http://localhost:8080
...
$ curl http://localhost:8080
Hello, "/"
json
package
$ touch json.go
// json.go
package main
import (
"encoding/json"
"fmt"
)
func main() {
str := []byte(`{"id":1,"name":"Gopher"}`)
data := struct {
ID int `json:"id"`
Name string `json:"name"`
}{}
if err := json.Unmarshal(str, &data); err != nil {
panic(data)
}
fmt.Println("ID: ", data.ID, "Name: ", data.Name)
}
$ go run json.go
ID: 1 Name: Gopher
Exercise 2-1
Create an application request.go
which creates a GET
request to https://api.github.com/users/defunkt and parses the response body with json.Unmarshal
only login
, id
, site_admin
, and bio
.
$ go run request.go
Login: defunkt
ID: 2
SiteAdmin: true
Bio: 🍔
The answer is request.go
Exercise 2-2
Create an application server.go
which builds a server. It has three endpoints, /
, /json
, and /404
. /
returns Hello, "/"
. /json
returns {"id":1,"name":"my_name","mail_address":""}
. /404
returns Not Found, /404
with the 404 status code.
$ go run server.go
http://localhost:8080
$ curl http://localhost:8080/
Hello, "/"
$ curl http://localhost:8080/json
{"id":1,"name":"my_name","mail_address":""}
$ curl http://localhost:8080/404
Not Found, /404
The answer is server.go.
💖 💪 🙅 🚩
Kohei
Posted on September 4, 2018
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
google Tough Task? Discover How Google’s AI Can Help You Understand Your Algorithm and Codes Faster.
November 10, 2024