package util

import (
	"bytes"
	"encoding/json"
	"errors"
	"io"
	"net/http"
)

func PostJsonResponseJson(url string, params map[string]string) (string, error) {
	jsonData, err := json.Marshal(params)
	if err != nil {
		return "", errors.New("参数转换成json失败，错误信息为：" + err.Error())
	}
	resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
	if err != nil {
		return "", errors.New("发送请求失败，错误信息为：" + err.Error())
	}
	defer func(Body io.ReadCloser) {
		_ = Body.Close()
	}(resp.Body)
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return "", errors.New("读取响应结果失败，错误信息为：" + err.Error())
	}
	return string(body), nil
}
