英文:
How to get response text when Got package crashes with bad JSON?
问题
抱歉,代码部分不要翻译。以下是翻译好的部分:
"Hi. I have an API endpoint that normally returns JSON, but sometimes there's an error and it prints something that breaks the JSON. Got just crashes with 'Unexpected token < in JSON at position 0'. How do I get the actual faulty text here?"
英文:
import got from "got";
try {
await got('https://google.com').json();
} catch (e) {
console.log(e);
// how do I get response text here?
}
Hi. I have an API endpoint that normally returns JSON, but sometimes there's an error and it prints something that breaks the JSON. Got just crashes with Unexpected token < in JSON at position 0
. How do I get the actual faulty text here?
答案1
得分: 1
根据got的文档,ParseError 包括一个 response
属性,您可以使用它来检索原始响应。
import got, { ParseError } from "got";
try {
await got('https://google.com').json();
} catch (e) {
console.log(e);
// 在这里如何获取响应文本?
if(e instanceof ParseError)
{
console.log(e.response.body);
}
}
英文:
As stated in got's documentation, ParseError includes a response
property, which you can use to retrieve the raw response.
import got, { ParseError } from "got";
try {
await got('https://google.com').json();
} catch (e) {
console.log(e);
// how do I get response text here?
if(e instanceof ParseError)
{
console.log(e.response.body);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论