Search code examples
restnode-red

Node-Red HTTP Input verify json


this might be a stupid question but I was unable to find a solution, also no luck with search.

My Node-Red flow gets triggered by a HTTP Input because I want to create a REST Webservice. It works fine so far but I wonder how to verify the content someone send to me.

As I see right now, I can pass any kind of content. There is no verification if the content matches to the content I want as input.

If I set content-type to application/json, it only accepts json data. That's great but I also want to ensure, users can post only a specific json string.

If someone sends data I'm unable to proceed (not the json object I expected), I want to send HTTP 400 as response.

I'm pretty new to Node-Red and also to json. I'm more an old-school programmer using classic webservices and soap. What I'm missing is some kind of WSDL (not sure if it exists when using REST) and some kind of payload validation.

It would be great if someone can point me the way to go.

Best regards Patrick


Solution

  • There are some nodes available for doing schema validation on JSON objects.

    For example, node-red-contrib-json-schema-validator - which uses ajv as the validation engine under the covers. Unfortunately this node doesn't come with much in the way of help. Essentially it lets you provide your schema and if a message fails to validate, it logs an error which can be handled with a Catch node if you want.