判断文件是否是stream还是json

  • 最简单的方式是检查响应的 Content-Type 头部,看它是否指定了 JSON 类型。大多数服务器会在返回 JSON 数据时设置正确的 Content-Type 头部为 application/json

const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('application/json')) { return response.json(); // 解析 JSON 数据 }
posted @ 2024-07-11 15:25  红苹果学园  阅读(2)  评论(0编辑  收藏  举报