dongxie559554 2018-10-19 17:12
浏览 837
已采纳

base64解码,然后json解码:base64.NewDecoder EOF错误和json无效字符错误

I'm trying to base64 decode HTTP request, then decode it using JSON decoder.

I tried two ways to implement base64 decoder:

func decode(encoded []byte) ([]byte, error) {
    buff := new(bytes.Buffer)
    decoder := base64.NewDecoder(base64.StdEncoding, buff)
    _, err := decoder.Read(encoded)

    return buff.Bytes(), err
}

This function returns an EOF error. Go playground link: https://play.golang.org/p/038rEXWYW6q


func decode(encoded []byte) ([]byte, error) {
    decoded := make([]byte, base64.StdEncoding.EncodedLen(len(encoded)))
    _, err := base64.StdEncoding.Decode(decoded, encoded)
    return decoded, err
}

This works but has extra x\00 characters so when decoding JSON we would have invalid character '\x00' after top-level value error.

Where is the problem with the first strategy?

  • 写回答

1条回答 默认 最新

  • douliaotong4944 2018-10-19 17:25
    关注

    In your code:

    You are creating a new buffer and assigning it to buff, since you have not given any source of input for it, it is empty.

    buff := new(bytes.Buffer)
    

    and NewDecoder is reading from empty buff. Like everything in go, if you want to create a something new, you should use it's constructor which always starts with package.Newxxx

    bytes.NewBuffer(src)
    

    Then decoder is a variable which contains the actual decoded data, and it has a reader interface(Read method). So you can pass it to method which accepts reader interface, and ioutil.ReadAll() is one of them.

    Added comments where ever necessary:

    package main
    
    import (
        "bytes"
        "encoding/base64"
        "fmt"
        "io/ioutil"
        "log"
    )
    
    // encoded data
    var data = "eyJhY3Rpdml0aWVzIjpbXSwic3VjY2VzcyI6ZmFsc2UsImNvZGUiOjk5OTl9"
    
    func main() {
        dec, err := decode([]byte(data))
        if err != nil {
            log.Println(err)
        }
        fmt.Println(string(dec)) // print returned value
    }
    
    func decode(enc []byte) ([]byte, error) {
        // create new buffer from enc
        // you can also use bytes.NewBuffer(enc)
        r := bytes.NewReader(enc)
        // pass it to NewDecoder so that it can read data
        dec := base64.NewDecoder(base64.StdEncoding, r)
        // read decoded data from dec to res
        res, err := ioutil.ReadAll(dec)
        return res, err
    }
    

    Infact the whole thing can be written in one line:

    func decode(enc []byte) ([]byte, error) {
        return ioutil.ReadAll(base64.NewDecoder(base64.StdEncoding, bytes.NewReader(enc)))
    }
    

    Output:

    {"activities":[],"success":false,"code":9999}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败