Get values from POST body

Hi,

I try to get POST values sent by Postman, but I get in a debug that the msg value is undefined. In the function module, I wrote a piece of such a code:
var u=msg.payload.username;
var p=msg.payload.password;
msg.payload=[u,p];
return msg;

When I change this code to:
var u=msg.payload[0];
var p=msg.payload[1];
msg.payload=[u,p];
return msg;
it returns integers: 42, 42.

How to properly get values sent by the postman in a body of POST?

Sorry, but we have nowhere near enough information to provide much help.

Are you saying that you are testing a node-red http-in node using postman?

If so, simply attach a debug node to the output of the http-in node and set it to show the whole msg.

If you want more help, you will need to share the config of the http-in node, the postman call and the debug output.

You probably get these numbers because they are the ASCII equivalent of the first two letters of the payload. In other words, your payload is a string and you need to set the http in node to parse the JSON into an object. Then you can use the built-in tools on the debug sidebar to grab the path to the element of interest.

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

So, my flow looks like this:


and info form debbugs is as follow:
c
The postman request and response is as follow:

Share your flow and I'll take a look.

Also, if you can, share a curl or build a http request in the demo flow to call the http node.


When posting 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 
```

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

JSON Flow file:

[
    {
        "id": "9da9a7c678366b94",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "2b7dfa6bd2f6c71b",
        "type": "template",
        "z": "9da9a7c678366b94",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "<html>  \n   <head>\n   </head>\n   <body>\n      <form action=\"/msprocess_login\" method=\"POST\">\n         <label for=\"username\">Login</label>\n         <input name=\"username\" type=\"text\" />\n         <br />\n         <label for=\"password\">Password</label>\n         <input name=\"password\" type=\"password\" />\n         <br />\n         <input type=\"submit\" />\n      </form>\n   </body>\n</html>  ",
        "output": "str",
        "x": 660,
        "y": 80,
        "wires": [
            [
                "e435327fb9c8b2ce"
            ]
        ]
    },
    {
        "id": "ca3084dfe4d33051",
        "type": "http response",
        "z": "9da9a7c678366b94",
        "name": "",
        "statusCode": "200",
        "headers": {},
        "x": 380,
        "y": 340,
        "wires": []
    },
    {
        "id": "d602177b36ede011",
        "type": "http in",
        "z": "9da9a7c678366b94",
        "name": "",
        "url": "/mslogin",
        "method": "get",
        "upload": false,
        "swaggerDoc": "",
        "x": 370,
        "y": 80,
        "wires": [
            [
                "2b7dfa6bd2f6c71b"
            ]
        ]
    },
    {
        "id": "e435327fb9c8b2ce",
        "type": "http response",
        "z": "9da9a7c678366b94",
        "name": "",
        "statusCode": "",
        "headers": {},
        "x": 890,
        "y": 80,
        "wires": []
    },
    {
        "id": "f634ee2176caee6f",
        "type": "http in",
        "z": "9da9a7c678366b94",
        "name": "",
        "url": "/msprocess_login",
        "method": "post",
        "upload": false,
        "swaggerDoc": "",
        "x": 400,
        "y": 220,
        "wires": [
            [
                "ca3084dfe4d33051",
                "b3cee598e1985fd4",
                "8bb8e3f5e79ea62d"
            ]
        ]
    },
    {
        "id": "66a33775a117738e",
        "type": "mysql",
        "z": "9da9a7c678366b94",
        "mydb": "aec8b21708539467",
        "name": "",
        "x": 930,
        "y": 220,
        "wires": [
            [
                "943ff9262e50ac79"
            ]
        ]
    },
    {
        "id": "b3cee598e1985fd4",
        "type": "function",
        "z": "9da9a7c678366b94",
        "name": "",
        "func": "var u=msg.payload.username;\nvar p=msg.payload.password;\nmsg.topic =\"select * from Users where username='\"+u+\"' and password='\"+p+\"'\";\nmsg.payload=[u,p];\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 680,
        "y": 220,
        "wires": [
            [
                "66a33775a117738e",
                "5f492585b0e0b787"
            ]
        ]
    },
    {
        "id": "5f492585b0e0b787",
        "type": "debug",
        "z": "9da9a7c678366b94",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 790,
        "y": 360,
        "wires": []
    },
    {
        "id": "943ff9262e50ac79",
        "type": "switch",
        "z": "9da9a7c678366b94",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "nempty"
            },
            {
                "t": "empty"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 1150,
        "y": 220,
        "wires": [
            [
                "a7e11d357d44e066"
            ],
            [
                "5613bc6e3ce46d86"
            ]
        ]
    },
    {
        "id": "a7e11d357d44e066",
        "type": "debug",
        "z": "9da9a7c678366b94",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1370,
        "y": 160,
        "wires": []
    },
    {
        "id": "5613bc6e3ce46d86",
        "type": "debug",
        "z": "9da9a7c678366b94",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1370,
        "y": 240,
        "wires": []
    },
    {
        "id": "8bb8e3f5e79ea62d",
        "type": "debug",
        "z": "9da9a7c678366b94",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 590,
        "y": 380,
        "wires": []
    },
    {
        "id": "aec8b21708539467",
        "type": "MySQLdatabase",
        "name": "",
        "host": "127.0.0.1",
        "port": "3306",
        "db": "MEC_Users",
        "tz": "",
        "charset": "UTF8"
    }
]

So, because you are using form data, you need to set the "file upload" option on the HTTP In node.

Here is a working example...

image

image
image

[{"id":"2b7dfa6bd2f6c71b","type":"template","z":"7eecf5f1d763605a","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>  \n   <head>\n   </head>\n   <body>\n      <form action=\"/msprocess_login\" method=\"POST\">\n         <label for=\"username\">Login</label>\n         <input name=\"username\" type=\"text\" />\n         <br />\n         <label for=\"password\">Password</label>\n         <input name=\"password\" type=\"password\" />\n         <br />\n         <input type=\"submit\" />\n      </form>\n   </body>\n</html>  ","output":"str","x":1508,"y":64,"wires":[["e435327fb9c8b2ce"]]},{"id":"ca3084dfe4d33051","type":"http response","z":"7eecf5f1d763605a","name":"","statusCode":"200","headers":{},"x":2188,"y":80,"wires":[]},{"id":"d602177b36ede011","type":"http in","z":"7eecf5f1d763605a","name":"","url":"/mslogin","method":"get","upload":false,"swaggerDoc":"","x":1322,"y":64,"wires":[["2b7dfa6bd2f6c71b"]]},{"id":"e435327fb9c8b2ce","type":"http response","z":"7eecf5f1d763605a","name":"","statusCode":"","headers":{},"x":1650,"y":64,"wires":[]},{"id":"f634ee2176caee6f","type":"http in","z":"7eecf5f1d763605a","name":"","url":"/msprocess_login","method":"post","upload":true,"swaggerDoc":"","x":1380,"y":160,"wires":[["8bb8e3f5e79ea62d","37d04cc256c5330a"]]},{"id":"5f492585b0e0b787","type":"debug","z":"7eecf5f1d763605a","name":"query","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"topic","targetType":"msg","statusVal":"topic","statusType":"auto","x":1762,"y":112,"wires":[]},{"id":"943ff9262e50ac79","type":"switch","z":"7eecf5f1d763605a","name":"is authorized?","property":"payload[0].authorized","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1984,"y":160,"wires":[["a7e11d357d44e066","ca3084dfe4d33051"],["5613bc6e3ce46d86","27f84a1c88e8574b"]]},{"id":"a7e11d357d44e066","type":"debug","z":"7eecf5f1d763605a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2202,"y":128,"wires":[]},{"id":"5613bc6e3ce46d86","type":"debug","z":"7eecf5f1d763605a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2202,"y":192,"wires":[]},{"id":"8bb8e3f5e79ea62d","type":"debug","z":"7eecf5f1d763605a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1426,"y":208,"wires":[]},{"id":"37d04cc256c5330a","type":"change","z":"7eecf5f1d763605a","name":"Set query","rules":[{"t":"set","p":"topic","pt":"msg","to":"select * from Users where username=:username and password=:password","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1588,"y":160,"wires":[["5f492585b0e0b787","cdbe2afb49f18417","2dc35737670e30b5"]]},{"id":"cdbe2afb49f18417","type":"debug","z":"7eecf5f1d763605a","name":"payload","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1772,"y":208,"wires":[]},{"id":"2dc35737670e30b5","type":"function","z":"7eecf5f1d763605a","name":"fake database","func":"const username = msg.payload.username;\nconst password = msg.payload.password;\n\nif (username == \"fred\" && password == \"wilma\") {\n    msg.payload = [{\n        username: username,\n        authorized: true\n    }]\n} else {\n    msg.payload = [{\n        username: username,\n        authorized: false\n    }]\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1792,"y":160,"wires":[["943ff9262e50ac79"]]},{"id":"27f84a1c88e8574b","type":"http response","z":"7eecf5f1d763605a","name":"","statusCode":"401","headers":{},"x":2196,"y":240,"wires":[]}]

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