Update README.md

dev
p6b5nefl7 9 months ago
parent 82629ad1b5
commit 789f73cc04

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

Loading…
Cancel
Save