golang处理get和post响应返回json数据

GET请求:简单写一个get请求接口

// get请求 http://localhost:9999/testGet
http.HandleFunc("/testGet", requestTestMethodsGet)

var resBodyMap map[int]map[string]string = make(map[int]map[string]string)
var count int = 0
var queryKey []string = []string{"id", "name", "age"}

// 请求处理逻辑函数
func requestTestMethodsGet(w http.ResponseWriter, r *http.Request) { 
	fmt.Println("Get请求执行了", r.URL.Query())
		queryMap := r.URL.Query()
		mapBody := make(map[string]string)

		// 使用切片配置query参数方式控制需要处理几个参数
		for _, v := range queryKey {
			mapBody[v] = queryMap.Get(v)
		}

		resBodyMap[count] = mapBody
		count++

		// 将response数据对象编码为 JSON 格式
		resDatas, _ := json.Marshal(resBodyMap)
		// 返回给浏览器
		io.WriteString(w, string(resDatas))
}


POST请求:简单写一个post请求接口


// post请求 http://localhost:9999/userInfo
http.HandleFunc("/userInfo", userInfoPost)

func userInfoPost(w http.ResponseWriter, r *http.Request) {
	// 请求post
	if r.Method == http.MethodPost {
		var param map[string]interface{}
		// 读取入参
		body, _ := ioutil.ReadAll(r.Body)
		// 将body二进制入参写入到param中
		json.Unmarshal(body, &param)
		
		resBody := ResponseBodys{
			Code: 200,
			Msg:  "sucess",
			Data: param,
		}

		// 将response数据对象编码为 JSON 格式 写入response
		b, _ := json.Marshal(resBody)
		io.WriteString(w, string(b))
	} else {
		w.WriteHeader(http.StatusMethodNotAllowed) //405 Method Not Allowed (HTTP 1.1 is the default status code for a method. It means that
	}
}

完整示例代码

package main

import (
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
)

type CompanyStruct struct {
	ID      int    `json:"id"`
	Name    string `json:"name"`
	Country string `json:"country"`
}

type ResponseBody struct {
	Code int           `json:"code"`
	Msg  string        `json:"msg"`
	Data CompanyStruct `json:"data"`
}

type ResponseBodys struct {
	Code int                    `json:"code"`
	Msg  string                 `json:"msg"`
	Data map[string]interface{} `json:"data"`
}

var resBodyMap map[int]map[string]string = make(map[int]map[string]string)
var count int = 0
var queryKey []string = []string{"id", "name", "age"}

func main() {
	// post请求 http://localhost:9999/testPost
	http.HandleFunc("/testPost", requestTestMethods)

	// post请求 http://localhost:9999/userInfo
	http.HandleFunc("/userInfo", userInfoPost)

	// get请求 http://localhost:9999/testGet
	http.HandleFunc("/testGet", requestTestMethods)

	// 服务启动端口 http://localhost:9999
	http.ListenAndServe(":9999", nil)
}

func requestTestMethods(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	// post请求
	case http.MethodPost:
		// 把body请求参数放到json容器
		Body := json.NewDecoder(r.Body)
		companyTest := CompanyStruct{
			ID:      0,
			Name:    "",
			Country: "",
		}
		// 将请求参数解码放到结构体里面
		err := Body.Decode(&companyTest)

		// 报错
		if err != nil {
			// 错误日志
			log.Println(err.Error())
			// 响应错误码给客户端 不应该出现错误消息的响应
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		// 正常执行
		enc := json.NewEncoder(w)
		// err2 := enc.Encode(companyTest)

		resBody := ResponseBody{
			Code: 200,
			Msg:  "接口请求成功",
			Data: companyTest,
		}
		err2 := enc.Encode(resBody)

		if err2 != nil {
			// 错误日志
			log.Println(err2.Error())
			// 响应错误码给客户端 不应该出现错误消息的响应
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

	// get请求
	case http.MethodGet:
		fmt.Println("Get请求执行了", r.URL.Query())
		queryMap := r.URL.Query()
		mapBody := make(map[string]string)

		// 这种赋值方式比较局限参数个数
		// mapBody["id"] = queryMap.Get("id")
		// mapBody["name"] = queryMap.Get("name")
		// mapBody["age"] = queryMap.Get("age")

		// 使用切片配置query参数方式控制需要处理几个参数
		for _, v := range queryKey {
			mapBody[v] = queryMap.Get(v)
		}

		resBodyMap[count] = mapBody
		count++

		// 将response数据对象编码为 JSON 格式
		resDatas, _ := json.Marshal(resBodyMap)
		// 返回给浏览器
		io.WriteString(w, string(resDatas))

	default:
		w.WriteHeader(http.StatusMethodNotAllowed)
	}
}

func userInfoPost(w http.ResponseWriter, r *http.Request) {
	// 请求post
	if r.Method == http.MethodPost {
		var param map[string]interface{}
		// 读取入参
		body, _ := ioutil.ReadAll(r.Body)
		// 将body二进制入参写入到param中
		json.Unmarshal(body, &param)
		//  map[country:北京市 id:100 name:Tyler Bennett]
		// fmt.Println(param)
		// fmt.Println(param["id"], param["name"], param["country"])

		resBody := ResponseBodys{
			Code: 200,
			Msg:  "sucess",
			Data: param,
		}

		// 将response数据对象编码为 JSON 格式
		// 返回给浏览器
		b, _ := json.Marshal(resBody)
		io.WriteString(w, string(b))
	} else {
		w.WriteHeader(http.StatusMethodNotAllowed) //405 Method Not Allowed (HTTP 1.1 is the default status code for a method. It means that
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追逐梦想之路_随笔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值