Ashkanxy
Ashkanxy

Reputation: 3941

How can we update a record by a http post method in GoLang?

Problem description:

I am learning Golang to implement the REST API for a small project. I was following this small example to get the some idea how to connect things. However, it looks like there are some bugs in the sample example, that i could not get the expected response in postman after hitting the endpoints. I have fixed it by adding the missing functions (HandleFunc functions) to make it work.

Problem Description:

However, I still have an issue with CreateEvent section. The expectation is that after using POST method with a given sample Event (json format) like below, event list is updated.

{
    "id": "23",
    "title": "This is simple Go lang title for test!",
    "Description":"In this course you will learn REST api implementation in Go lang"

}

But after reaching the "http://localhost:8080/events" endpoint in which I defined to return all the events (1 defined inside code, the other should be added by calling CreateEvent function) i get only one of the event (hard coded one inside code only) in response.

Here is the complete code. I appreciate for any suggestions/comments.

package main

import (
        "fmt"
        "log"
        "net/http"
        "io/ioutil"
    
    "encoding/json"
        "github.com/gorilla/mux"
)

func homeLink(w http.ResponseWriter, r *http.Request) {
        fmt.Println("test started!")
        fmt.Fprintf(w, "Welcome home!")
}

func main() {
        router := mux.NewRouter().StrictSlash(true)
        router.HandleFunc("/", homeLink)
/*i have added the next 3 lines, missing in the sample code*/
        router.HandleFunc("/event", createEvent)
        router.HandleFunc("/events/{id}", getOneEvent)
        router.HandleFunc("/events", getAllEvents)
        log.Fatal(http.ListenAndServe(":8080", router))
}

type event struct {
    ID          string `json:"ID"`
    Title       string `json:"Title"`
    Description string `json:"Description"`
}

type allEvents []event

var events = allEvents{
    {
        ID:          "1",
        Title:       "Introduction to Golang",
        Description: "Come join us for a chance to learn how golang works and get to eventually try it out",
    },
}

func createEvent(w http.ResponseWriter, r *http.Request) {
    var newEvent event
    reqBody, err := ioutil.ReadAll(r.Body)
    if err != nil {
        fmt.Fprintf(w, "Kindly enter data with the event title and description only in order to update")
    }

        fmt.Println("Create Event is called!")
    json.Unmarshal(reqBody, &newEvent)
    events = append(events, newEvent)
    w.WriteHeader(http.StatusCreated)

    json.NewEncoder(w).Encode(newEvent)
}

func getOneEvent(w http.ResponseWriter, r *http.Request) {
    eventID := mux.Vars(r)["id"]

        fmt.Println("get one event is called!")
        fmt.Println(eventID)
    for _, singleEvent := range events {
        if singleEvent.ID == eventID {
            json.NewEncoder(w).Encode(singleEvent)
        }
    }
}


func getAllEvents(w http.ResponseWriter, r *http.Request) {

        fmt.Println("Get all events is called!")
    json.NewEncoder(w).Encode(events)
}

Upvotes: 0

Views: 1315

Answers (1)

nipuna
nipuna

Reputation: 4095

Your code is working fine. I have tested it (just copied above code and ran in my local machine and tested with Postman).

Btw, i have added few recommendations for a better code below.

If there is not nil error, handle it and return.

reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
    fmt.Fprintf(w, "Kindly enter data with the event title and description only in order to update")
    return //add this return, otherwise continue the function with the error
}

Put this json error handling for createEvent Handler function

err = json.Unmarshal(reqBody, &newEvent)
if err != nil {
    fmt.Fprintf(w, "json format invalid")
    return
}

Add http methods to your endpoints.

router.HandleFunc("/", homeLink).Methods(http.MethodGet)
/*i have added the next 3 lines, missing in the sample code*/
router.HandleFunc("/event", createEvent).Methods(http.MethodPost)
router.HandleFunc("/events/{id}", getOneEvent).Methods(http.MethodGet)
router.HandleFunc("/events", getAllEvents).Methods(http.MethodGet)

Upvotes: 1

Related Questions