Here is an example of POST request body:
{"action":"do_something","id":"001"}
I took an example of simple json parser
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type some_json struct {
Action string `json:"action"`
Id string `json:"id"`
}
func jsonparse(rw http.ResponseWriter, request *http.Request) {
decoder := json.NewDecoder(request.Body)
var post_data some_json
err := decoder.Decode(&post_data)
if err != nil {
panic(err)
}
switch ***WHAT_SHOULD_BE_HERE???*** {
default :
fmt.Fprintf(w,"WRONG PARAM")
case "some_thing":
fmt.Fprintf(w,post_data.Id + "
")
}
}
func main() {
http.HandleFunc("/", jsonparse)
http.ListenAndServe(":8080", nil)
}
I already know how to switch cases from form values, but how to switch cases of json key values?