You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NodeJS如何解析HTTP Multipart x-mixed-replace格式的连续响应

Hey there! Let's break down how to handle this multipart/x-mixed-replace stream from your security system API. I’ve dealt with similar streaming endpoints before, so here’s a practical approach depending on the language you’re using (I’ll cover the most common ones):

Python Approach

Python's requests library makes streaming HTTP responses straightforward. Here's how to capture the event data into variables:

  1. First, ensure you have requests installed (pip install requests if you don't already).
  2. Use stream=True in your request to keep the connection open for the continuous stream.
  3. Iterate over response chunks, handling cases where the boundary splits across chunks (super common with streaming).
import requests

# Replace with your API details
API_URL = "https://your-security-api.com/events-stream"
AUTH_HEADERS = {"Authorization": "Bearer YOUR_AUTH_TOKEN"}
BOUNDARY = b"--DummyBoundary"  # Match the exact boundary from the API docs

# Initialize variables to track events
events = []
current_part = b""

response = requests.get(API_URL, headers=AUTH_HEADERS, stream=True)
response.raise_for_status()  # Catch HTTP errors early

for chunk in response.iter_content(chunk_size=1024):
    if not chunk:
        continue
    
    # Split the incoming chunk using the boundary
    split_parts = chunk.split(BOUNDARY)
    
    for part in split_parts:
        if current_part:
            # Combine leftover data from the last chunk with the current part
            full_event = current_part + part
            # Clean up leading/trailing newlines (standard multipart formatting)
            cleaned_event = full_event.strip()
            if cleaned_event:
                # Convert to string if your data is text-based, or keep as bytes for binary
                events.append(cleaned_event.decode("utf-8"))
                # Do something with the event here—log it, process it, etc.
                print(f"Captured event: {cleaned_event[:50]}...")
            current_part = b""
        else:
            # If this is a partial part, save it to combine with the next chunk
            if not part.endswith(b"\r\n\r\n"):
                current_part = part
            else:
                # Full event received in one chunk
                cleaned_event = part.strip()
                events.append(cleaned_event.decode("utf-8"))

Key Notes for Python:

  • Adjust chunk_size based on your event size—1024 bytes works for most cases, but you can tweak it for performance.
  • If your events are binary (like images), skip the .decode("utf-8") step and keep the data as bytes.
  • Add error handling for connection drops (wrap the loop in a try/except block to restart the stream if needed).

Node.js Approach

For Node.js, you can use the native http/https module or libraries like axios to handle the stream. Here's a native implementation:

const https = require("https");

// Replace with your API details
const options = {
  hostname: "your-security-api.com",
  path: "/events-stream",
  headers: { Authorization: "Bearer YOUR_AUTH_TOKEN" },
};

const BOUNDARY = "--DummyBoundary";
let buffer = "";
const events = [];

https.get(options, (res) => {
  res.on("data", (chunk) => {
    buffer += chunk.toString();
    // Split the buffer by the boundary
    const parts = buffer.split(BOUNDARY);
    // Keep the last (incomplete) part for the next chunk
    buffer = parts.pop();

    parts.forEach((part) => {
      const cleanedPart = part.trim();
      if (!cleanedPart) return;

      // Separate headers from event content (standard multipart uses \r\n\r\n as the separator)
      const [headers, content] = cleanedPart.split("\r\n\r\n");
      if (content) {
        events.push(content);
        // Process the event here—parse JSON, store in a database, etc.
        console.log(`Captured event: ${content.slice(0, 50)}...`);
      }
    });
  });

  res.on("error", (err) => {
    console.error("Stream connection error:", err);
    // Add logic to reconnect here if needed
  });
});

Key Notes for Node.js:

  • The native module avoids extra dependencies, but if you prefer axios, you can use its responseType: 'stream' option and handle the data events similarly.
  • If dealing with binary data, use Buffer instead of converting chunks to strings.
  • Implement reconnection logic if the stream drops unexpectedly—security systems often keep streams open indefinitely until interrupted.

General Best Practices

  • Verify the Boundary: Double-check that your boundary string exactly matches what the API sends (sometimes responses include extra parameters in the boundary, but your docs specify --DummyBoundary so stick to that).
  • Parse Event Data: If events are JSON, use json.loads() (Python) or JSON.parse() (Node.js) to convert the content into usable objects.
  • Memory Management: If you're capturing a lot of events over time, consider processing them immediately instead of storing all in an array to avoid memory bloat.

内容的提问来源于stack exchange,提问作者Julio Castro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:11:22