|
|
|
@ -7,43 +7,97 @@ import (
|
|
|
|
|
"net/http"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func getRequest(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// 这里是你的getRequest函数的实现...
|
|
|
|
|
// 根据请求方法返回不同的JSON响应
|
|
|
|
|
// ...
|
|
|
|
|
|
|
|
|
|
// 示例:对于GET请求,返回一个简单的JSON响应
|
|
|
|
|
if r.Method == http.MethodGet {
|
|
|
|
|
if r.URL.Query().Get("ID") == "" {
|
|
|
|
|
http.Error(w, "ERROR: id is null", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp := struct {
|
|
|
|
|
ID string `json:"ID"`
|
|
|
|
|
Name string `json:"Name"`
|
|
|
|
|
}{
|
|
|
|
|
ID: r.URL.Query().Get("ID"),
|
|
|
|
|
Name: "GET Request Example",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jsonResp, err := json.Marshal(resp)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
_, err = io.WriteString(w, string(jsonResp))
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("Error writing response:", err)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ... 其他请求方法的处理 ...
|
|
|
|
|
}
|
|
|
|
|
func getRequest(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
switch r.Method {
|
|
|
|
|
case http.MethodGet:
|
|
|
|
|
|
|
|
|
|
if r.URL.Query().Get("ID") == "" {
|
|
|
|
|
http.Error(w,"ERROR : id is null",http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
marshal, _ := json.Marshal(struct {
|
|
|
|
|
ID string `json:"ID"`
|
|
|
|
|
Name string `json:"Name"`
|
|
|
|
|
}{
|
|
|
|
|
ID: r.URL.Query().Get("ID"),
|
|
|
|
|
Name: "PostMan GET Method",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
|
io.WriteString(w, string(marshal[:]))
|
|
|
|
|
break
|
|
|
|
|
case http.MethodPost:
|
|
|
|
|
|
|
|
|
|
var data map[string]interface{}
|
|
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, &data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, "can not Unmarshal", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
marshal, _ := json.Marshal(struct {
|
|
|
|
|
Body map[string]interface{} `json:"Body"`
|
|
|
|
|
}{
|
|
|
|
|
Body: data,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
|
io.WriteString(w, string(marshal[:]))
|
|
|
|
|
break
|
|
|
|
|
case http.MethodPut:
|
|
|
|
|
|
|
|
|
|
var data map[string]interface{}
|
|
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, &data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, "can not Unmarshal", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
marshal, _ := json.Marshal(struct {
|
|
|
|
|
ID string `json:"ID"`
|
|
|
|
|
Name string `json:"Name"`
|
|
|
|
|
}{
|
|
|
|
|
ID: data["ID"].(string),
|
|
|
|
|
Name: "PostMan PUT Method",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
|
io.WriteString(w, string(marshal[:]))
|
|
|
|
|
break
|
|
|
|
|
case http.MethodDelete:
|
|
|
|
|
|
|
|
|
|
if r.URL.Query().Get("ID") == "" {
|
|
|
|
|
http.Error(w,"ERROR : id is null",http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
marshal, _ := json.Marshal(struct {
|
|
|
|
|
Name string `json:"Name"`
|
|
|
|
|
}{
|
|
|
|
|
Name: "PostMan DELETE Method",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
|
io.WriteString(w, string(marshal[:]))
|
|
|
|
|
break
|
|
|
|
|
default:
|
|
|
|
|
log.Println("ERROR: Invalid HTTP Method")
|
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
// 将你的请求处理程序绑定到/test路径
|
|
|
|
|