Request JSON

Untuk memperoleh request JSON bisa memanfaatkan JSON Decoder dan Marshal seperti code berikut.

Contoh code request JSON dengan map sebagai penampung data

package main

import (
	"encoding/json"
	"fmt"
	"net/http"
)

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/welcome", func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "POST" {
			var req map[string]interface{}
			err := json.NewDecoder(r.Body).Decode(&req)
			if err != nil {
				w.WriteHeader(http.StatusBadRequest)
			}
			jsonByte, err := json.Marshal(req)
			if err != nil {
				w.WriteHeader(http.StatusInternalServerError)
			}
			w.Write(jsonByte)
		}
	})

	server := http.Server{
		Addr: 	"localhost:5000",
		Handler: mux,
	}

	fmt.Println("Server running on", server.Addr)
	err := server.ListenAndServe()
	if err != nil {
		panic(err)
	}
}

Contoh request JSON dengan struct sebagai penampung data

package main

import (
	"encoding/json"
	"fmt"
	"net/http"
)

type User struct {
	Username string
	Password string
}

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/welcome", func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "POST" {
			var req User
			err := json.NewDecoder(r.Body).Decode(&req)
			if err != nil {
				w.WriteHeader(http.StatusBadRequest)
			}
			jsonByte, err := json.Marshal(req)
			if err != nil {
				w.WriteHeader(http.StatusInternalServerError)
			}
			w.Write(jsonByte)
		}
	})

	server := http.Server{
		Addr: 	"localhost:5000",
		Handler: mux,
	}

	fmt.Println("Server running on", server.Addr)
	err := server.ListenAndServe()
	if err != nil {
		panic(err)
	}
}

Last updated