我使用Golang读取Node JS POST API,当我通过 Postman 打它工作正常,但通过node js不工作日志打印**{“menPhoto”:[交易中心**只.我尝试了下面的步骤,但没有使用可以任何一个帮助它,谢谢.
请求正文
{
"menPhoto": [ //Dynamic key
"Trading centre/TCXY7079/ASSES/9088c44x-c1ds-482r-8c4b-df2cc86f7b32_name.jpg"
],
"womenPhoto": [ //Dynamic key
"Trading centre/TCXY7080/ASSES/9088c44y-c1ds-482r-8c4b-df2cc86f7b32_name.jpg"
],
"menIDPhoto": [ //Dynamic key
"Trading centre/TCXX7080/ASSES/9088c44y-c1ds-482r-8c4b-df2cc86f7b32_name.jpg"
]
}
字符串
Node JS
getImages: gI => {
axios.post(config.dcmtService, reqUrls, {
headers: headers
}).then((response) => {
})
}
型
GoLang
第一次尝试
var data map[string]interface{} or map[string][]string
err = json.NewDecoder(r.Body).Decode(&data)
fmt.Println("1=============>>>>", data) //empty
if err != nil {
fmt.Println("GetSignedURLs err " + err.Error())
}
型
二次尝试
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))
Response - {"menPhoto": [Trading centr
型
第三次尝试encodeURIComponent(在node js end)
Node
-------------------------------------------------------
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())
}
Response - {"menPhoto": [Trading centr
型
2条答案
按热度按时间cpjpxq1n1#
您可以使用Unmarshal:
字符串
qqrboqgw2#
你可以按照下面的方法操作。你可以在代码中用r.Body替换
strings.NewReader(data)
字符串
检查我在这里创建的goplayground https://go.dev/play/p/TeyaSHx1L2r