Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "encoding/json"
- "io/ioutil"
- "log"
- "net/http"
- "github.com/gorilla/mux"
- )
- type Book struct {
- title string `json:"title"`
- content string `json:"content"`
- }
- var books []Book
- func createBook(w http.ResponseWriter, r *http.Request) {
- reqBody, _ := ioutil.ReadAll(r.Body)
- var book Book
- json.Unmarshal(reqBody, &book)
- books = append(books, book)
- log.Print(&book)
- json.NewEncoder(w).Encode(book)
- }
- func getBook(w http.ResponseWriter, r *http.Request) {
- json.NewEncoder(w).Encode(books)
- }
- func main() {
- router := mux.NewRouter().StrictSlash(true)
- router.HandleFunc("/", createBook).Methods("POST")
- router.HandleFunc("/", getBook).Methods("GET")
- log.Fatal(http.ListenAndServe(":8081", router))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement