You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
1 year ago | |
---|---|---|
README.md | 1 year ago |
README.md
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)
}
}