Skip to main content

http-response

Send HTTP responses back to clients from http-in or http-webhook nodes.

Overview

The http-response node sends an HTTP response back to the client that made the original request. It must be paired with an http-in or http-webhook node. The response body is taken from msg.payload, and the status code, headers, and cookies can all be configured statically or set dynamically via message properties.

Status
Code control
Headers
Custom headers
JSON
Auto serialize
Pair
With http-in
Required Pairing

Every http-in or http-webhook node must have a corresponding http-response node in its flow. Without it, HTTP clients will timeout waiting for a response. The msg.res object from the input node must be preserved and passed through to this node.

Properties

Property Type Default Description
statusCode number 200 HTTP status code. Can be overridden by msg.statusCode.

Dynamic Message Properties

Set these properties on the incoming message to control the response dynamically:

msg Property Type Description
msg.payload any Response body. Objects are auto-serialized as JSON.
msg.statusCode number Overrides the configured status code (e.g., 201, 404, 500).
msg.headers object Custom response headers as key-value pairs.
msg.cookies object Set response cookies. Each key is a cookie name.

Example: REST API Response with JSON Data

Return sensor readings as a JSON API response. The http-in node receives the request, a function node builds the response data, and http-response sends it back.

// Function node before http-response:
const readings = flow.get('sensorReadings') || [];
msg.payload = {
  status: "ok",
  count: readings.length,
  data: readings,
  timestamp: new Date().toISOString()
};
msg.headers = {
  "Content-Type": "application/json",
  "Cache-Control": "no-cache"
};
msg.statusCode = 200;
return msg;

// http-response node sends:
// HTTP/1.1 200 OK
// Content-Type: application/json
// Cache-Control: no-cache
//
// {
//   "status": "ok",
//   "count": 5,
//   "data": [...],
//   "timestamp": "2024-01-15T14:30:00Z"
// }

Example: Custom Error Responses

Return different HTTP error codes based on validation results. Use a switch node to route success and error paths to separate http-response nodes or set msg.statusCode dynamically.

// Function node with error handling:
const data = msg.payload;

// Validate required fields
if (!data || !data.sensor_id) {
  msg.statusCode = 400;
  msg.payload = {
    error: "Bad Request",
    message: "sensor_id is required"
  };
  return msg;
}

// Check if sensor exists
const sensor = flow.get('sensors.' + data.sensor_id);
if (!sensor) {
  msg.statusCode = 404;
  msg.payload = {
    error: "Not Found",
    message: "Sensor " + data.sensor_id + " not found"
  };
  return msg;
}

// Check authorization
if (msg.req.headers['x-api-key'] !== env.get('API_KEY')) {
  msg.statusCode = 401;
  msg.payload = {
    error: "Unauthorized",
    message: "Invalid API key"
  };
  return msg;
}

// Success
msg.statusCode = 200;
msg.payload = { status: "ok", sensor: sensor };
return msg;

Common Status Codes

2xx Success

200 - OK (default)

201 - Created (after POST)

204 - No Content (empty response)

4xx Client Errors

400 - Bad Request

401 - Unauthorized

404 - Not Found

429 - Too Many Requests

Common Use Cases

REST API Endpoints

Return JSON data from sensor queries and CRUD operations

Webhook Acknowledgment

Send 200 OK to confirm webhook receipt to services

Error Handling

Return proper HTTP error codes with descriptive messages

File Downloads

Serve files with appropriate Content-Disposition headers

Related Nodes