1 回答

TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊
根據(jù)我的評論,我的意思是,在這一行中,value最有可能是類型[]byte(或[]uint8- 這是同一件事)
value, found := ristrettoCache.Get(key)
JSON 編碼 a[]byte將隱式地 base64 輸出 - 因?yàn)?JSON 是基于文本的。
json.NewEncoder(w).Encode(value) // <- value is of type []byte
檢查您發(fā)布的 base64 ( https://play.golang.org/p/NAVS4qRfDM2 ) 底層二進(jìn)制字節(jié)已經(jīng)用 JSON 編碼 - 所以不需要額外json.Encode的。
只需在處理程序中輸出原始字節(jié) - 并將內(nèi)容類型設(shè)置為application/json:
func getTokenInfo(w http.ResponseWriter, r *http.Request){
vars := mux.Vars(r)
key := vars["chain"]+vars["symbol"]
value, found := ristrettoCache.Get(key)
if !found {
return
}
// json.NewEncoder(w).Encode(value) // not this
w.Header().Set("Content-Type", "application/json")
if bs, ok := value.([]byte); ok {
_, err := w.Write(bs) //raw bytes (already encoded in JSON)
// check 'err'
} else {
// error unexpected type behind interface{}
}
}
添加回答
舉報(bào)