How do I access the response body from an error? #1991
-
|
I am calling an API that can responds with HTTP code 409 and a response body as below. I tried calling this api using got as below. But the logs doesn't show the body. Is it possible to catch it? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
The |
Beta Was this translation helpful? Give feedback.
-
|
Here's how I ended up with a much clearer logged error for my use case: Use it like so: ✌️ |
Beta Was this translation helpful? Give feedback.
-
|
A simpler answer 🙂: try {
const response = await got.put(`my-url-here`, {
json: payload,
})
.json();
} catch (error) {
console.log(error.response.body); // that's where you will get your body text from.
} |
Beta Was this translation helpful? Give feedback.
The
response, meaning also thebody, is attached to the error. It's all in the docs. The response is not visible when logging for practical reasons (the object is huge) and security reasons.