From 82629ad1b5c0dfb2a2dfa6f418578bf3ece535f0 Mon Sep 17 00:00:00 2001 From: p6b5nefl7 <3047923646@qq.com> Date: Tue, 21 May 2024 01:41:47 +0800 Subject: [PATCH] Update README.md --- README.md | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 576c717..c089863 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,58 @@ -# test +package main + +import ( + "encoding/json" + "fmt" + "io" + "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 main() { + // 将你的请求处理程序绑定到/test路径 + http.HandleFunc("/test", getRequest) + + // 监听1024端口(注意:可能需要root权限或使用高于1024的端口) + fmt.Println("Starting server on port 1024...") + if err := http.ListenAndServe(":1024", nil); err != nil { + fmt.Println("Error starting server:", err) + } +}