无法读取golang中的node js请求正文

oxcyiej7  于 2023-11-14  发布在  Go
关注(0)|答案(2)|浏览(123)

我使用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

cpjpxq1n

cpjpxq1n1#

您可以使用Unmarshal:

type JsonRsp struct {
    MenPhoto     []string          `json:"menPhoto"`
    WomenPhoto   []string          `json:"womenPhoto"`
    MenIDPhoto   []string          `json:"menIDPhoto"`
}

jsonResult := JsonRsp{}

body, err := io.ReadAll(r.Body)
if err != nil {
    return
}

err = json.Unmarshal(body, &jsonResult)
if err != nil {
    return
}

fmt.Println("menPhoto:", jsonResult.MenPhoto[0])

字符串

qqrboqgw

qqrboqgw2#

你可以按照下面的方法操作。你可以在代码中用r.Body替换strings.NewReader(data)

package main

 import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "strings"
 )

var data = `{
"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"
]
 }`

 func main() {
  body, err := ioutil.ReadAll(strings.NewReader(data))
if err != nil {
    fmt.Println(err.Error())
}

var response map[string]interface{}
err = json.Unmarshal(body, &body)
if err != nil {
    return
}
fmt.Println("1=============>>>>", response)
}

字符串
检查我在这里创建的goplayground https://go.dev/play/p/TeyaSHx1L2r

相关问题