package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
)
func main() {
//设置路由规则
http.HandleFunc("/", handlePostForm)
//使用默认的DefaultServeMux
//err := http.ListenAndServe(":45678", nil)
err := http.ListenAndServe(":8102", nil)
//err := http.ListenAndServe(":8707", nil)
if err != nil {
log.Fatal(err)
}
}
func handlePostForm(writer http.ResponseWriter, request *http.Request) {
fmt.Println(request.Method)
buf := bytes.NewBuffer(make([]byte, 0, 512))
length, _ := buf.ReadFrom(request.Body)
fmt.Println(string(buf.Bytes()))
fmt.Println(length)
type sendMsg struct {
Code string `json:"code"`
Message string `json:"message"`
Data struct
{
Message string `json:"message"`
}`json:"data"`
}
var sendBodyMsg sendMsg
sendBodyMsg.Code = "200"
sendBodyMsg.Message = "ok"
sendBodyMsg.Data.Message = "success"
bt, _ := json.Marshal(sendBodyMsg)
//body := ioutil.NopCloser(strings.NewReader(string(bt)))
io.WriteString(writer, string(bt))
//io.WriteString(w,"123456")
}
go语言-httpServer
最新推荐文章于 2024-08-09 13:29:49 发布