Handling HTTPRequest Error

Dear All,

Currently I am hitting http request to get the response. When API is down , I am not able to handle the exception. I am getting "JSON parse error".

Catch block is not triggering , nor I am getting any http outpout as I have switch node to check the statusCode.

Please help, Thanks in advance.


Output a utf string, then check msg.statusCode in the response message and only allow the ones you want to pass, i.e 200, then pass through a json node.

I tried and still getting the "JSON parse error"

What data are you getting? Has the error moved to the JSON node? Use a debug to see what is being passed to the JSON node. My guess is your getting html not JSON.

To add to Steve's questions, what are the statusCodes received when working and when down?
How is the catch node set up?
Set all debugs nodes to complete message object
How are you checking for Statuscodes?

You need to supply info so we can diagnose.

@Steve-Mcl @E1cid I am getting below data in payload and 404 as statusCode

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<pre>Cannot POST /post/data</pre>

statusCode : 404

So what is the problem then? Use that to indicate the error. Obviously do not pass messages with error to the JSON node.

I thought you had a basic understanding of node-red standard nodes. You may get some benefit from watching these essentials videos

To the issue.
Add a switch node between the http and the JSON nodes. set property to statusCode and the rule to == 200 the 200 is a number not a string.

I am doing the same, output of httprequest is linked with switch node. but no output is coming, request is not flowing to switch node and getting an exception "JSPN parse error"

Please export your flow and paste here
How to import/export a flow

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

   code goes here 

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.