我有一个API,在其中我使用lz4帧压缩来压缩字符串,并在响应中将结果发送到前端。在JavaScript中,我获取该API并使用pierrec的lz4包解压缩数据,当时我得到了无效的幻数错误。有人能帮我吗?
后端
package main
import (
"bytes"
"fmt"
"io"
"github.com/pierrec/lz4"
"github.com/gin-gonic/gin"
)
var fileContent = `CompressBlock compresses the source buffer starting at soffet into the destination one. This is the fast version of LZ4 compression and also the default one. The size of the compressed data is returned. If it is 0 and no error, then the data is incompressible. An error is returned if the destination buffer is too small.`
func main() {
router := gin.Default()
// Define the API endpoint
router.POST("/api/endpoint", func(c *gin.Context) {
toCompress := []byte(fileContent)
// compressed := make([]byte, len(toCompress))
result, err := compress(toCompress)
if err != nil {
fmt.Println(err)
}
c.JSON(200, gin.H{"result": result})
})
// Start the server
router.Run(":8000")
}
func compress(in []byte) ([]byte, error) {
r := bytes.NewReader(in)
w := &bytes.Buffer{}
zw := lz4.NewWriter(w)
_, err := io.Copy(zw, r)
if err != nil {
return nil, err
}
// Closing is very important
if err := zw.Close(); err != nil {
return nil, err
}
return w.Bytes(), nil
}
字符串
前端
import fetch from 'node-fetch';
import LZ4 from 'lz4';
const apiUrl = 'http://localhost:8000/api/endpoint';
const postData = async () => {
try {
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({}),
});
if (!response.ok) {
throw new Error('Request failed with status ' + response.status);
}
const result = await response.json();
// const decoded = base64ToArrayBuffer(result.result);
const buffer = Buffer.from(result.result);
const decompressed = LZ4.decode(buffer);
const decodedBuffer = Buffer.from(decompressed);
console.log(decodedBuffer.toString());
} catch (error) {
console.error(error);
}
};
postData();
型
错误类型
Error: Invalid magic number: 4D466941 @0
at Decoder.emit_Error (/home/anand/go/src/github.com/anand-dotworld/test/client/node_modules/lz4/lib/decoder_stream.js:64:22)
at Decoder.read_MagicNumber (/home/anand/go/src/github.com/anand-dotworld/test/client/node_modules/lz4/lib/decoder_stream.js:93:8)
at Decoder._main (/home/anand/go/src/github.com/anand-dotworld/test/client/node_modules/lz4/lib/decoder_stream.js:289:25)
at Decoder._transform (/home/anand/go/src/github.com/anand-dotworld/test/client/node_modules/lz4/lib/decoder_stream.js:60:7)
at Decoder.Transform._write (node:internal/streams/transform:184:23)
at writeOrBuffer (node:internal/streams/writable:389:12)
at _write (node:internal/streams/writable:330:10)
at Decoder.Writable.end (node:internal/streams/writable:609:17)
at Object.LZ4_uncompress [as decode] (/home/anand/go/src/github.com/anand-dotworld/test/client/node_modules/lz4/lib/decoder.js:14:10)
at postData (file:///home/anand/go/src/github.com/anand-dotworld/test/client/index.js:24:30)
型
谢谢你的好意
1条答案
按热度按时间iibxawm41#
JavaScript代码中有一个错误。你应该换掉
字符串
与
型
下面是json的文档。Marshal:
数组和切片值编码为JSON数组,除了[]byte编码为base64编码的字符串,nil切片编码为null JSON值。
您可以看到后端代码将
[]byte
编码为base64编码的字符串。但是根据Buffer.from(string[,encoding])的文档,
encoding
的默认值是utf8
。因此,为了解码base64编码的字符串,您应该将编码指定为base64
explicilty。我测试了这个配置:
go.mod:
型
package.json:
型
使用以下命令运行js代码:
型