You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Golang读取Node.js POST请求体不完整问题求助

Golang对接Node.js POST API请求体读取不完整问题

用Golang对接Node.js的POST API,Postman调用时一切正常,但通过Node.js发起调用时,Golang端日志仅打印{"menPhoto": [Trading centr,请求体读取不完整。已尝试以下三种方案均未解决,求排查:

请求体示例

{
    "menPhoto": [ //动态键名
        "Trading centre/TCXY7079/ASSES/9088c44x-c1ds-482r-8c4b-df2cc86f7b32_name.jpg"
    ],
    "womenPhoto": [ //动态键名
        "Trading centre/TCXY7080/ASSES/9088c44y-c1ds-482r-8c4b-df2cc86f7b32_name.jpg"
    ],
    "menIDPhoto": [ //动态键名
        "Trading centre/TCXX7080/ASSES/9088c44y-c1ds-482r-8c4b-df2cc86f7b32_name.jpg"
    ]
}

Node.js请求代码

getImages: gI => {
axios.post(config.dcmtService, reqUrls, {
  headers: headers
}).then((response) => {

})
}

已尝试的三种方案

方案1:直接用JSON Decoder解析

var data map[string]interface{} // 或 map[string][]string
err = json.NewDecoder(r.Body).Decode(&data)
fmt.Println("1=============>>>>", data) // 输出为空
if err != nil {
    fmt.Println("GetSignedURLs err " + err.Error())
}

结果:解析后data为空。

方案2:直接读取请求体

body, err := ioutil.ReadAll(r.Body)
if err != nil {
    http.Error(w, "Error reading request body", http.StatusBadRequest)
    return
}

fmt.Println("Request body string:", string(body))
// 输出结果:{"menPhoto": [Trading centr

结果:读取到的请求体不完整,截断在"Trading centr"处。

方案3:Node.js端编码后Golang端解码

Node.js代码

const encodedString = encodeURIComponent(reqUrls);
getImages: gI => {
axios.post(config.dcmtService, encodedString, {
  headers: headers
}).then((response) => {

})
}

Golang代码

body, err := ioutil.ReadAll(r.Body)
if err != nil {
    http.Error(w, "Error reading request body", http.StatusBadRequest)
    return
}

decodedString, err := url.QueryUnescape(string(body))
if err != nil {
    http.Error(w, err.Error(), http.StatusBadRequest)
    return
}

fmt.Println("Decoded string:", decodedString)

var data map[string]interface{}
err = json.NewDecoder(r.Body).Decode(&data)
fmt.Println("1=============>>>>", data)
if err != nil {
    fmt.Println("GetSignedURLs err " + err.Error())
}
// 输出结果:{"menPhoto": [Trading centr

结果:解码后的字符串依然截断,解析data也失败。


内容的提问来源于stack exchange,提问作者udayakumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 04:26:35