52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package main
|
||
|
||
import (
|
||
"bytes"
|
||
"encoding/json"
|
||
"fmt"
|
||
"io"
|
||
"net/http"
|
||
)
|
||
|
||
func main() {
|
||
// 定义请求的 URL
|
||
url := "http://127.0.0.1:12419/mall.Category/Fetch"
|
||
|
||
// 设置正确的请求体(根据接口要求)
|
||
requestBody := []byte(`{"store_identity":"01965b51-5344-7218-a9b9-bc71031f6f10"}`)
|
||
|
||
// 创建 POST 请求
|
||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
|
||
if err != nil {
|
||
fmt.Println("创建请求失败:", err)
|
||
return
|
||
}
|
||
|
||
// 设置请求头(通常需要 application/json)
|
||
req.Header.Set("Content-Type", "application/json")
|
||
|
||
// 发起请求
|
||
client := &http.Client{}
|
||
resp, err := client.Do(req)
|
||
if err != nil {
|
||
fmt.Println("请求失败:", err)
|
||
return
|
||
}
|
||
defer resp.Body.Close()
|
||
|
||
// 读取响应体
|
||
body, err := io.ReadAll(resp.Body)
|
||
if err != nil {
|
||
fmt.Println("读取响应失败:", err)
|
||
return
|
||
}
|
||
data := map[string]any{}
|
||
|
||
json.Unmarshal(body, &data)
|
||
result := data["result"].(map[string]any)
|
||
|
||
// 打印响应结果
|
||
fmt.Println("响应状态码:", resp.Status)
|
||
fmt.Println("响应内容:", result["data"])
|
||
}
|