dt_automate/conn/parsers.go
2025-02-19 18:30:19 +08:00

31 lines
716 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package conn
import (
"bytes"
"encoding/json"
"encoding/xml"
"net/http"
)
// parsers.go 主要包含了一些通用的格式转换、解析操作
// ScanXml 按xml格式解析resp的payloadBody
func ScanXml(resp *http.Response, v interface{}) error {
return xml.NewDecoder(resp.Body).Decode(v)
}
// ScanJson 按json格式解析resp的payloadBody
func ScanJson(resp *http.Response, v interface{}) error {
return json.NewDecoder(resp.Body).Decode(v)
}
// ToJsonBuff 转成 json 格式的 buff作为 http body
func ToJsonBuff(v any) (*bytes.Buffer, error) {
var b bytes.Buffer
e := json.NewEncoder(&b)
// 设置禁止html转义
e.SetEscapeHTML(false)
err := e.Encode(v)
return &b, err
}