无法在 go 中使用 base64 编码的字节片解组 json

Can't unmarshal json with base64 encoded byte slice in go

我很难解组从 AWS lambda 函数发送给我的结构。

无论我做什么,所有 pkidata 字段都是空的。我创建了这个代码示例来隔离进程:

package main

import (
    "encoding/base64"
    "encoding/json"
    "fmt"
)

func main() {
    mystring := `{"error":{"errormessage": "errormessagetest", "errortype": "errortypetest"}, "pkidata":{"certificate":"Q2VydGlmaWNhdGU6CiAgICBEYXRhOgogICAgICAgIFZlcnNpb246IDMgKDB4MikKICAgICAgICBTZXJpYWwgTnVtYmVyOgogICAgICAgICAgICBhYTo5MzpiMTo4MDphYjpiYTplYzo5ZTo4MzpmOTo2OTo5NToyNDpkZjo5Njo2OQogICAgU2lnbmF0dXJlIEFsZ29yaXRobTogc2hhMjU2V2l0aFJTQUVuY3J5cHRpb24KICAgICAgICBJc3N1ZXI6IENOPXJhcGhvZXN0ZXIKICAgICAgICBWYWxpZGl0eQogICAgICAgICAgICBOb3QgQmVmb3JlOiBNYXIgMjcgMTk6NDM6MTQgMjAyMiBHTVQKICAgICAgICAgICAgTm90IEFmdGVyIDogSnVuIDI5IDE5OjQzOjE0IDIwMjQgR01UCiAgICAgICAgU3ViamVjdDogQ049cmFwaG9lc3RlcgogICAgICAgIFN1YmplY3QgUHVibGljIEtleSBJbmZvOgogICAgICAgICAgICBQdWJsaWMgS2V5IEFsZ29yaXRobTogcnNhRW5jcnlwdGlvbgogICAgICAgICAgICAgICAgUHVibGljLUtleTogKDIwNDggYml0KQogICAgICAgICAgICAgICAgTW9kdWx1czoKICAgICAgICAgICAgICAgICAgICAwMDpjNjplZjpkNjoxYTo0Mjo0Mjo2ZTo5MTpiODpmNTpiZjoyNToyNTpkODoKICAgICAgICAgICAgICAgICAgICBhNjo4Yjo4MzpmYjo2ZDo4MzpiNjowZTo3Mzo4ZjowYTozMjo5ZjpjNjplMjoKICAgICAgICAgICAgICAgICAgICBmMTo4Njo5NDoxMzo5MjplYjo0MTozNTo4NTpiZDo0ZDpkNzowNzo4ZTo5YjoKICAgICAgICAgICAgICAgICAgICBjYzoyYzpkMjo2Nzo5ZDpjOTo1Mzo4Yjo4ODpjZToxMTozOTpkODpkZTo3MjoKICAgICAgICAgICAgICAgICAgICBhMTphODo4MDozMDpmOTpiYTpiOTo3NTpjNTphMjoxYjpiZToyMDo3Yzo1ZjoKICAgICAgICAgICAgICAgICAgICBhMjpjZjpiNzphMjo2ODo4MzpiMzoxMTplODoyNTo3Mzo2MzoyNjphNzpiODoKICAgICAgICAgICAgICAgICAgICBjMzpjZDpjODplZTozNjphZjo4ZTo4ZjowZTo5NDowMToyMDpmZjowZTo0MToKICAgICAgICAgICAgICAgICAgICBjYjplZDpmYjo3NDowNToyZDo0NDphOToxNjoxZDo0ODpkNDphNDo3NTo0ZjoKICAgICAgICAgICAgICAgICAgICBlZjo0NDozYTphMDpmMDo1ZTo5ZTo5YjoxOTphZTo4MTozMzpkMDpkMTpmYzoKICAgICAgICAgICAgICAgICAgICA0MTphMTo1OTo5Zjo3MDoxZDphYTo3MzoyNTpiNTpiNjo3NDo2Mzo1NzpjZjoKICAgICAgICAgICAgICAgICAgICAzNTplNzpiZToyNjo1MzoxNDpkNTplNzpiMzpmYToyZDpjYzo5NToyMzo0ODoKICAgICAgICAgICAgICAgICAgICBkOTpkNTozNDplMjowNzoxYjoyNzoyMzpiZTpiODpmNzpiNTpkOTo3MDo1ZDoKICAgICAgICAgICAgICAgICAgICAyOTo4NTo3NToyYjpmYTo0MjpmNTo2Mjo1OTozZjowODo3Njo0Nzo2NjpmNzoKICAgICAgICAgICAgICAgICAgICAxYzpiYzozNzo1MjpiMDo3ODoyNzozNDo3MTo3ZTpiNjozZDowNDpjZDoyMzoKICAgICAgICAgICAgICAgICAgICAzMjphNjoxYTo2NzphNDo4ODoyYTowYTpmMjoxMjphNTo3ODpkYjpiZDpjOToKICAgICAgICAgICAgICAgICAgICA1ZToxMTo4Njo0MDpkMTo0MTplZjpkYzo2Yjo4Zjo1NjpmZDo1MjpkYTo0ZjoKICAgICAgICAgICAgICAgICAgICBiNTpmMDozYTpmZTpjNzoxMDo3ZTpjNDo3OTo2MTo1ZTpkYjo5Zjo4NjpjOToKICAgICAgICAgICAgICAgICAgICBmYjpiNwogICAgICAgICAgICAgICAgRXhwb25lbnQ6IDY1NTM3ICgweDEwMDAxKQogICAgICAgIFg1MDl2MyBleHRlbnNpb25zOgogICAgICAgICAgICBYNTA5djMgQmFzaWMgQ29uc3RyYWludHM6IAogICAgICAgICAgICAgICAgQ0E6RkFMU0UKICAgICAgICAgICAgWDUwOXYzIFN1YmplY3QgS2V5IElkZW50aWZpZXI6IAogICAgICAgICAgICAgICAgMDU6M0Y6RDk6MjE6Rjg6MUE6QjM6NDM6OEQ6QzI6NDA6NzI6OUY6MDE6NDg6MTc6NDE6Nzc6Q0U6MzkKICAgICAgICAgICAgWDUwOXYzIEF1dGhvcml0eSBLZXkgSWRlbnRpZmllcjogCiAgICAgICAgICAgICAgICBrZXlpZDozNTpCRjpBOTozRDo1RjozMzozODowQjowNjo3QzpDQTpFQzo0ODo3QzpCRTo3Njo3MzpCRTpEQjpGRQogICAgICAgICAgICAgICAgRGlyTmFtZTovQ049cmFwaG9lc3RlcgogICAgICAgICAgICAgICAgc2VyaWFsOjhGOjE3OkY1OjJBOjhBOkI0OkQwOjY5CgogICAgICAgICAgICBYNTA5djMgRXh0ZW5kZWQgS2V5IFVzYWdlOiAKICAgICAgICAgICAgICAgIFRMUyBXZWIgU2VydmVyIEF1dGhlbnRpY2F0aW9uCiAgICAgICAgICAgIFg1MDl2MyBLZXkgVXNhZ2U6IAogICAgICAgICAgICAgICAgRGlnaXRhbCBTaWduYXR1cmUsIEtleSBFbmNpcGhlcm1lbnQKICAgICAgICAgICAgWDUwOXYzIFN1YmplY3QgQWx0ZXJuYXRpdmUgTmFtZTogCiAgICAgICAgICAgICAgICBETlM6cmFwaG9lc3RlcgogICAgU2lnbmF0dXJlIEFsZ29yaXRobTogc2hhMjU2V2l0aFJTQUVuY3J5cH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VVmaWcxN2dZdUR3WC9nMlMKVklDMVJDdWxwVTNORnNLOEhrSDdjdVFPOGNjSnlWc3JtUndPSFhabmtwTnBoYXI4RFQvSjU5d3FNU09ZZUJQYgpuQU41T0piaUF0NlRBNmRsUXJidkFvWG04dXdHMjFSU1N1VHQ5UElsY3NGallQazhiQWJ0dXdldEVnYjVuNkhoClhhczA1S0dteENmZDZxNHAxYUxITkVpNTVSejg0bEVsVHVNZlA0ZEwyZjIzUnFncmpEQmNDRmEwN1owZHJNdjcKbERYNEovaVpXY08zWXJJWXppcVg5M015UzNSUCtrYXAvV0dIYlZDcWtpZ2lTODZwQkY1NDh3OWxXQ0xJai9kTgozRTI2V24zNgotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg=="}}`
// yeah the string is long lol 
    var body ResponseBody
    if err := json.Unmarshal([]byte(mystring), &body); err != nil {
        fmt.Println(fmt.Errorf("failed unmarshaling response | %s", err.Error()))
    }

    fmt.Println(body.Error.ErrorMessage)

    certChain, err := base64.StdEncoding.DecodeString(mystring)
    if err != nil {
        fmt.Println(err.Error())
    }
    fmt.Println(certChain)
}

type PkiGenerationInput struct {
    TeacherUsername string `json:"teacherusername"`
}

type Response struct {
    Headers ResponseHeaders `json:"headers"`
    Body    ResponseBody    `json:"body"`
}

type ResponseHeaders struct {
    ContentType string `json:"Content-Type"`
}

type ResponseBody struct {
    PkiData ParsablePkiData `json:"pkidata"`
    Error   struct {
        ErrorMessage string `json:"errormessage"`
        ErrorType    string `json:"errortype"`
    } `json:"error"`
}

type ParsablePkiData struct {
    Certificate      string `json:"certificate"`
    PrivateKey       string `json:"privatekey"`
    CertificateChain string `json:"certificatechain"`
}

这是输出

errormessagetest
illegal base64 data at input byte 0
[]

我可以阅读 errormessagetest 的事实表明解组有点有效,所以这很奇怪。

我检查了所有未导出的字段,但我们可以清楚地看到它们都是大写的。我还“只是为了看看”减小了字符串大小,但它没有改变任何东西。

综合所有评论的答案:

首先,我的“愚蠢错误”是因为我试图解码错误的变量:

// mystring needs to be replaced by body.PkiData.Certificate
base64.StdEncoding.DecodeString(mystring) 

但研究让我们发现,当结果类型与声明的类型匹配时,json.Unmarshal() 函数会自行解码 base64。鉴于编码数据是 []byte,我只是用结构中的 []byte 替换了 string 类型。

这允许创建以前代码的更简单版本:

type ResponseBody struct {
    PkiData PkiData `json:"pkidata"`
}

type PkiData struct {
    Certificate      []byte
    PrivateKey       []byte
    CertificateChain []byte
}

var body ResponseBody
if err := json.Unmarshal(rawResp.Payload, &body); err != nil {
    return PkiData{}, fmt.Errorf("failed unmarshaling response | %s", err.Error())
}