Wappler Version : v5.x
Operating System : N/A
Server Model: NodeJS
Database Type: N/A
Hosting Type: N/A
Situation
@jellederijke has to perform an API Action to a third-party API, that answers an HTTP 200 OK
status code and Content-Type application/json
, however, the response body is empty.
Actual behavior
Error is logged to console due to failure to decode JSON due to empty body. Error is logged, NOT THROWN:
if (res.headers['content-type'] && res.headers['content-type'].includes('json')) {
try {
body = JSON.parse(body);
} catch (e) {
console.error(e);
}
}
Desired behaviour:
If response body is empty, do not attempt to JSON decode, even if Content-Type application/json
is set (see modified 1st line):
if (res.headers['content-type'] && res.headers['content-type'].includes('json') && body) {
try {
body = JSON.parse(body);
} catch (e) {
console.error(e);
}
}
Additionally, instead of logging error to console, THROW the error.