javascript - 如何解压缩 Gunzip 以及如何将对象解析为 Json?
问题描述
我需要将个人资料图片上传到个人资料。我需要获取 ID,因此我将图片上传到正确的个人资料。API 的响应是一个 Gunzip 对象。
.then(resp => JSON.parse(resp)).then(data => {console.log(data.Gunzip.read(5)} )
我已经尝试过了,但出现错误:(node:16100) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token o in JSON at position 1
const fetch = require("node-fetch");
const userID = '12345'
const EXAMPLE_PUT_URL = `https://backend.example.com/api/users/${userID}`
const EXAMPLE_GET_URL = `https://backend.example.com/api/users/?limit=5`
const ACCESS_TOKEN = 'NWNmNmYyOWMzNTk5YTA3YjU3MGZkN2Y2OmV6UXo1TiZnXmVtWmJfKzRsOFJ5LWFMLShUUzFzbno7dDNpLV5KX0dOLFIwT1dkTFh1VFZWalE2LFNtKX4pV3U='
var fs = require('fs');
var contents = fs.readFileSync('images/profile1.jpg', 'utf8');
fetch(EXAMPLE_GET_URL, {
method: 'GET',
headers: {
"Authorization": `Basic ${ACCESS_TOKEN}`,
"Content-type": "application/json",
"Accept": "application/json",
"Accept-Charset": "utf-8",
},
})
.then(response => {
fetch(EXAMPLE_PUT_URL, {
method: 'PUT',
headers: {
"Authorization": `Basic ${ACCESS_TOKEN}`,
"Content-type": "application/json",
"Accept": "application/json",
"Accept-Charset": "utf-8",
},
body: JSON.stringify({
avatar:contents,
})
})
console.log(response)})
;
使用此代码,我得到了压缩对象而不是对象,所以我看不到我需要的 ID。
解决方案
推荐阅读
- php - 预订系统的 PHP 对象/数组算法
- python - Django rest 框架简单模型序列化器列表视图需要永远显示 10 条记录
- apache-nifi - 如何在 Apache NIFI 中将机器学习应用于流数据
- regex - Notepad++ 正则表达式 - 如何从此列表中删除代理?
- sas - 将Stata“egen,group”转换为SAS
- kubernetes - kubernetes 容器中的 redis-cluster 在 spinup 时重新启动
- angular - Angular 订阅功能未在 http 404 错误上触发
- javascript - Modify Apollo React function to use parameters
- r - 如何根据多组标准找到平均值、最大值和最小值
- javascript - 将数组写入javascript中的元素的未知错误?