"illegal base64 data" 尝试 base64 解码时

"illegal base64 data" when trying to base64 decode

我尝试在 Go 中解码一个有效的(根据我的理解)base64 编码的字符串:

data, err := base64.StdEncoding.DecodeString(s)
if err != nil {
     ...
}

一个完整的example is here. I have a string "eyJlbWFpbF9hZGRyZXNzIjoiIiwiZXhwIjoxNDQ3NzIzMzY4LCJmaXJzdG5hbWUiOiIiLCJpYXQiOjE0NDc0NjQxNjgsImlzcyI6Imh0dHA6Ly91ZGFjaXR5LmNvbSIsImtpZCI6ImE3ZTg5ZWQyMSIsImxhc3RuYW1lIjoiIiwidXNlcl9pZCI6IjEyMzQ1Njc4IiwidXNlcm5hbWUiOiJoYW5zb2xvQGhvdGguY29tIn0", which can be properly decoded for example here 甚至在您的浏览器控制台中使用 atob(that_string);,但由于某些原因 go 抱怨:

illegal base64 data at input byte 236

注意,我可以解码其他一些字符串。那么为什么我不能在 Go 中使用 base64 解码有效的编码字符串?

您的输入没有任何padding. Therefore, you should use base64.RawStdEncoding over base64.StdEncoding:

data, err := base64.RawStdEncoding.DecodeString(s)

示例:https://play.golang.org/p/ZWfzYXQ5Ye