link1644 link1645 link1646 link1647 link1648 link1649 link1650 link1651 link1652 link1653 link1654 link1655 link1656 link1657 link1658 link1659 link1660 link1661 link1662 link1663 link1664 link1665 link1666 link1667 link1668 link1669 link1670 link1671 link1672 link1673 link1674 link1675 link1676 link1677 link1678 link1679 link1680 link1681 link1682 link1683 link1684 link1685 link1686 link1687 link1688 link1689 link1690 link1691 link1692 link1693 link1694 link1695 link1696 link1697 link1698 link1699 link1700 link1701 link1702 link1703 link1704 link1705 link1706 link1707 link1708 link1709 link1710 link1711 link1712 link1713 link1714 link1715 link1716 link1717 link1718 link1719 link1720 link1721 link1722 link1723 link1724 link1725 link1726 link1727 link1728 link1729 link1730 link1731 link1732 link1733 link1734 link1735 link1736 link1737 link1738 link1739 link1740 link1741 link1742 link1743 link1744 link1745 link1746 link1747 link1748 link1749 link1750 link1751 link1752 link1753 link1754 link1755 link1756 link1757 link1758 link1759 link1760 link1761 link1762 link1763 link1764 link1765 link1766 link1767 link1768 link1769 link1770 link1771 link1772 link1773 link1774 link1775 link1776 link1777 link1778 link1779 link1780

[Vue.js] Axios Request - Gzip data from PHP API

is it possible to gzcompress data in PHP and then have Axios request it?

I’ve tried doing this but keep getting this error: “Malformed UTF-8 characters, possibly incorrectly encoded.”

My Axios request looks like this:

method: ‘get’,
url: ‘',
data: { },
config: { headers: { ‘Content-Type’: ‘application/json’, ‘Accept-Encoding’: ‘gzip’ }
.then(response => { => {
this.transactionsFull = this.transactions;
this.loading = false;
.catch(e => {

$result = openssl_decrypt($cipher_text, ‘aes-256-gcm’, $key, OPENSSL_RAW_DATA, $iv, $auth_tag);

$json = json_decode($result);
$channel = Channel::where(‘uuid’, $json->payload->authentication->entityId)->first();
$gzencode = gzencode(json_encode(array(‘transaction’ => $json, ‘relation’ => json_decode($channel))), 8);

Redis::lpush(‘transactions_gzencode’, $gzencode);

$length = 0;
$transactions = Redis::lrange(‘transactions_gzencode’, 0, -1);
foreach($transactions as $item) {
$length += strlen($item);
header(‘Content-Encoding: gzip’);
header(‘Content-Type: application/json’);
header(‘Content-Length: ‘ . $length);
return $transactions;

Solution :

I believe that axios is not able to decompress gzip, but the browser should be able to do it before axios even touches the response. But for the browser to do so you must respond with the proper http header and format.

Note that to use the compressed data in the http response body you must use gzencode, not gzcompress, according to php documentation.

Example PHP:

$compressed = gzencode(json_encode([‘test’ => 123]));
header(‘Content-Type: application/json’);
header(‘Content-Encoding: gzip’);
header(‘Content-Length: ‘ . strlen($compressed));
echo $compressed;

Example JS:

console.log(await (await fetch(‘/test’)).json());
// {test: 123}


Since what you are trying to do is to send an array of individually compressed items, you can output the data in a JSON encoded array of base64 encoded binary compressed data.

Example of how to use pako.js to decompress the array of compressed transactions returned from the server:


$transactions = [‘first’, ‘second’, ‘third’];
echo json_encode(array_map(‘base64_encode’, array_map(‘gzencode’, $transactions)));


(async () => {
const transactions = (await (await fetch(‘/test’)).json())
.map(blob => pako.inflate(blob, { to: ‘string’ }));


Notice that now I didn’t include the headers, cause when just sending a regular json encoded array.

The downside of this approach is that there won’t be much benefit on compressing the data, since it is being converted to base64 before sending to the client. It is necessary to encode as base64, because otherwise json_encode would try to handle the binary data as string and it would lead to string encoding errors.

You can still compress the resulting json encoded string before sending to the client like was done I the previous answer, but I’m not sure if the compression would still be good enough:

$compressedTransactions = array_map(‘gzencode’, [‘first’, ‘second’, ‘third’]);

$compressed = gzencode(json_encode(array_map(‘base64_encode’, $compressedTransactions)));
header(‘Content-Type: application/json’);
header(‘Content-Encoding: gzip’);
header(‘Content-Length: ‘ . strlen($compressed));
echo $compressed;