How to send a post request with content

I want to use a button to trigger a post request with additional content, how to achieve it


As shown in the figure, it contains multiple headers and a JSON content

Inject (or button) --> change node (set msg.payload to JSON with your content) --> http out node (set to post and add any headers you need)

The change node will push the JSON to the http out node via msg.payload

Just for further clarification, that’s the “HTTP request” node. HTTP-in and HTTP-response are used for serving content through flows.

Good catch.

Very good. I have achieved my goal. Thank you very much.

Hello, I have a new problem


I use HTTP nodes to post data in node-red, but the annotated places are not valid json data, so I tried many methods

check the content type in your screenshot, and start there.

I used the Google browser developer tool to view the form data and then used the HTTP node at node-red. I had filled in the request header, but the form data would not be submitted

I think you're using the wrong node. The one in your second screenshot is the HTTP Response node. You're looking for the HTTP Request node, and set headers to it through a change node to msg.headers, or a function node doing the same.

image
I rearranged the process, but it didn't get me where I wanted to go

without knowing how the rest of your flow looks like I can't give you much advice on that... On top of that, I'm far from sure that hardcoding the session cookie would be a good idea. Better would it be to have the login process done in node-red as well, save the return cookies and use those going forward.

Aha, I will not set the login and save the cookie, but I have tested it for a day, and the same cookie can be used in multiple IP addresses. I am not sure whether there will be any problem in the future. I provided the json file during the learning stage, please check if you have timeflows 6.0.json (56.0 KB)

you can send it with ReqBin, quickly and easily, right from your browser.

Hello - I'm trying to do exactly this but I am not sure how to use the saved cookies in the next http request node. I can have Nodered log in and I can see the cookies being returned in message.responseCookie. Any guidance on how to use that in the next http request would be really appreciated. I'm somewhat of a novice so please be patient :slightly_smiling_face:

Take a look at the docs for the http-request node. It says that the cookies from the request come back as msg.responseCookies, but to put them on a request you need msg.cookies. So you put a change node in between, and make something that sets responseCookies to cookies. I do so with JSONata, and if needed merge it with an existing set of cookies: Set msg.cookies to $exists($.cookies) ? $merge([cookies, responseCookies]) : responseCookies
It checks if msg.cookies is already defined and if so merges the cookies object with the incoming responseCookies, and if not sets the cookies to responseCookies.

1 Like

Thanks for the response @afelix. I tried the following and I don't think it is working... below is the test flow:

I can see the cookie returned from the first http request node

Cookie in first login/response - msg.responseCookies

{"HUBSESSION":{"Path":"/","value":"node086xpcac4lcdvi67wbof0gyj871.node0"}}

In the function node, I set msg.cookies to msg.responseCookies

Function node

var cookies = msg.responseCookies;
msg.headers={};
msg.cookies = cookies;
return msg;

The output of the function node is:

Function Node output - msg.cookies

{"HUBSESSION":{"Path":"/","value":"node086xpcac4lcdvi67wbof0gyj871.node0"}}

The second http request node is supposed to use this cookie and download a file (currently I have it set to UTF-8 string, but normally it would be binary)


When I look at the return http headers from this request, there is a different value in cookie?

http header

{"connection":"close","date":"Thu, 20 Aug 2020 12:48:03 GMT","set-cookie":["HUBSESSION=**node0149tbnewmz54xbtn971u3a20m72.node0;**Path=/"],"expires":"Thu, 01 Jan 1970 00:00:00 GMT","x-frame-options":"DENY","content-type":"text/html;charset=utf-8","server":"Jetty(9.4.z-SNAPSHOT)","x-node-red-request-node":"007887aa"}

I have a curl script (copied from someone else - I don't know curl) that does this and it works - just trying to make it work with Node-red

Curl Script

curl -c cookie.txt -d "username=USERNAME" -d "password=PASSWORD" http://HUB_IP/login
curl -sb cookie.txt http://HUB_IP/hub/backupDB?fileName=latest -o "/Users/NAME/Documents/Hubitat_Backup/Hubitat_Backup.lzf"

Thanks for any pointers..

Hello,

I've also a problem with my garbage collector date.
This is the Url:

https://www.rova.nl/api/TrashCalendar/GetCalendarItems?portal=inwoners

And this is the cookie:

RovaLc_inwoners={"Id":XXXXX,"ZipCode":"XXXXX","HouseNumber":"XX","HouseAddition":"","Municipality":"XXXXXXXXX","Province":null,"Firstname":null,"Lastname":null,"UserAgent":"","School":null,"Street":"Schout Doddestraat","IsRovaArea":true,"Country":null,"Portal":null,"Lat":"XXXXXXXXX","Lng":"XXXXXXX667","AreaLevel":X,"City":"XXXXXXXXXX","Ip":null}

I have read the above but I can't find a solution. I tried with the change node (set msg.payload to JSON with my content) but it's not valid JSON content.

And how to add the header to the http request node?

HTTP POST doesn't work this is the error:

payload: "{"Message":"The requested resource does not support http method 'POST'."}"