Detailed explanation of various requests in the HTTP request node of Node-RED

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

Detailed explanation of various requests in the HTTP request node of Node-RED

Introduction

In Node-RED, if you want to request external resources, such as obtaining local weather information, you can use the HTTP request node. This node allows your Node-RED application to integrate various interfaces and data. It is not limited by cross-domain restrictions and can access most HTTP interfaces. It supports a wide range of request methods, including GET, PUT, POST, PATCH, or DELETE. Additionally, it supports custom headers, cookies, request address templating, file uploading, request timeouts, and various other powerful features. This document will detail its various usages to help you quickly grasp and improve your work efficiency.

Overview of Node Configuration

This node has both inputs and outputs.
The input configuration items include these:

urlIf not configured in the node, this optional attribute sets the request URL.
methodIf not configured in the node, this optional attribute sets the request HTTP method. It must be one of GET, PUT, POST, PATCH, or DELETE.
headersSets the request HTTP headers.
cookiesIf set, it can be used to send cookies with the request.
payloadSent as the request body.
rejectUnauthorizedIf set to false, it allows requests to https sites using self-signed certificates.
followRedirectsIf set to false, it prevents following redirects (HTTP 301). The default is true
requestTimeoutIf set to a positive number of milliseconds, it will overwrite the globally set httpRequestTimeout parameter.

Output parameter

payloadThe body of the response. The node can be configured to return the body as a string, attempt to parse it as a JSON string, or retain it as a binary buffer.
statusCodeThe status code of the response; if the request cannot be completed, an error code will be returned.
headersAn object containing the response headers.
responseUrlIf any redirects occur during the processing of the request, this property will be the final redirected URL. Otherwise, it will be the original request URL.
responseCookiesIf the response contains cookies, this property is an object of 'name/value' key-value pairs for each cookie.
redirectListIf the request is redirected one or more times, the accumulated information will be added to this property. 'location' is the next redirect target. 'cookie' is the cookie returned from the redirect source.

The above is the translated official English documentation. Let's look at some specific special usages.

Use code to configure the http request node

The input parameters of this node can be configured in the configuration panel,

but they can also be set in the flow.
For example, if you want to make a GET request to thehttps://fizzz.blog.csdn.net/address,

you can use a function node before the HTTP request interface to set the request parameters

msg.method = 'GET'
msg.url = 'https://fizzz.blog.csdn.net/'

. In addition, you can also set multiple custom headers
, as shown in the example

msg.method = 'GET'
msg.url = 'https://fizzz.blog.csdn.net/?name=fizz&age=18'
msg.headers = {}
msg.headers['X-Auth-User'] = 'fizz'
msg.headers['X-Auth-Key'] = 'fizz-key'

. In the configuration parameters of the HTTP request node, you can use templates to reference variables in msg
. Here's how to configure the URL
https://{{domin}}.blog.csdn.net/

. The variabledominis injected into msg from upstreammsg.domin.

. When using a POST request, the parameters should be set as follows

msg.method = 'POST'
msg.url = 'https://fizzz.blog.csdn.net/?name=fizz&age=18'
msg.payload = {}
msg.payload.name = 'fizz'
msg.payload.key = 'fizz-key'

. Form data, file upload

If the data is form data, msg.headers["content-type"] should be set toapplication/x-www-form-urlencoded.

For the scenario of uploading files, special settings are required for the http request node.

To perform file upload, msg.headers["content-type"] should be set tomultipart/form-dataandmsg.payload. The object passed to the node must have the following structure:

{
    "KEY": {
        "value": FILE_CONTENTS,
        "options": {
            "filename": "FILENAME"
        }
    }
}

The values of KEY, FILE_CONTENTS, and FILENAME should be set to appropriate values.

Related Tags
Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.