Skip to content

Instantly share code, notes, and snippets.

@alyssaq
Last active June 20, 2018 03:58
Show Gist options
  • Save alyssaq/a67282c2603ec2259d63 to your computer and use it in GitHub Desktop.
Save alyssaq/a67282c2603ec2259d63 to your computer and use it in GitHub Desktop.
GET and POST golang API
/*
* Sample API with GET and POST endpoint.
* POST data is converted to string and saved in internal memory.
* GET endpoint returns all strings in an array.
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
var (
// flagPort is the open port the application listens on
flagPort = flag.String("port", "9000", "Port to listen on")
)
var results []string
// GetHandler handles the index route
func GetHandler(w http.ResponseWriter, r *http.Request) {
jsonBody, err := json.Marshal(results)
if err != nil {
http.Error(w, "Error converting results to json",
http.StatusInternalServerError)
}
w.Write(jsonBody)
}
// PostHandler converts post request body to string
func PostHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body",
http.StatusInternalServerError)
}
results = append(results, string(body))
fmt.Fprint(w, "POST done")
} else {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
}
}
func init() {
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
flag.Parse()
}
func main() {
results = append(results, time.Now().Format(time.RFC3339))
mux := http.NewServeMux()
mux.HandleFunc("/", GetHandler)
mux.HandleFunc("/post", PostHandler)
log.Printf("listening on port %s", *flagPort)
log.Fatal(http.ListenAndServe(":"+*flagPort, mux))
}

To run:

> go main.go
> go main.go  -port=1234  // custom port

Open browser at localhost:9000

post:

> curl localhost:9000 -d "[1, 2, 3]"

get

> curl localhost:9000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment