AlexxIT/go2rtc · error

multipart: wrong boundary:

Error message

multipart: wrong boundary: 

What it means

go2rtc's MJPEG-over-HTTP (multipart/jpeg) reader expects each part separator line of the multipart stream to start with "--" (the boundary prefix). If a line read before a part does not look like a boundary delimiter, the server is not speaking the expected multipart format, so Next fails with the offending line appended to the message.

Solutions

  1. Verify the URL is the actual MJPEG stream endpoint (often /video.mjpg or /mjpg/video.mjpeg), not a snapshot or admin page.
  2. Check the camera's HTTP response with curl (`curl -s URL | head`) to confirm it returns multipart/jpeg with boundary lines.
  3. Update go2rtc — known vendor quirks (Foscam G2) have workarounds added.
  4. Confirm the camera requires authentication; an unauthenticated request may return an HTML login page that fails this check.

Example fix

// before: pointing at snapshot endpoint
url := "http://camera/ipcam.jpg"
// after: use the MJPEG stream endpoint
url := "http://camera/mjpg/video.mjpeg"
Defensive patterns

Strategy: validation

Validate before calling

resp, _ := http.Get(url)
ct := resp.Header.Get("Content-Type")
if !strings.HasPrefix(ct, "multipart/x-mixed-replace") {
    return fmt.Errorf("expected multipart stream, got %s", ct)
}

Try / catch

producer, err := mpjpeg.NewProducer(transport)
if err != nil {
    if strings.Contains(err.Error(), "wrong boundary") {
        return fmt.Errorf("URL is not an MJPEG multipart stream; check endpoint and auth: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling Producer.Start (via mpjpeg probe) against an HTTP endpoint whose response body lines between JPEG frames do not begin with "--boundary" — e.g. the server sent an error page, HTML, or a non-multipart body.

Common situations: Wrong URL pointing to a snapshot page or HTML error page instead of the MJPEG stream; camera returning HTTP 200 with an error body; firmware quirks (e.g. Foscam cameras sending odd separators, see go2rtc issue #1258); proxy stripping multipart framing.

Related errors


AI-assisted analysis of AlexxIT/go2rtc@c245815e75 (2026-09-07). Data as JSON: /api/errors/e345998e9e03c2f9. Report an issue: GitHub.

Appendix: source

Thrown at pkg/mpjpeg/multipart.go:26

	"net/textproto"
	"strconv"
	"strings"
)

func Next(rd *bufio.Reader) (http.Header, []byte, error) {
	for {
		// search next boundary and skip empty lines
		s, err := rd.ReadString('\n')
		if err != nil {
			return nil, nil, err
		}

		if s == "\r\n" {
			continue
		}

		if !strings.HasPrefix(s, "--") {
			return nil, nil, errors.New("multipart: wrong boundary: " + s)
		}

		// Foscam G2 has a awful implementation of MJPEG
		// https://github.com/AlexxIT/go2rtc/issues/1258
		if b, _ := rd.Peek(2); string(b) == "--" {
			continue
		}

		break
	}

	tp := textproto.NewReader(rd)
	header, err := tp.ReadMIMEHeader()
	if err != nil {
		return nil, nil, err
	}

	s := header.Get("Content-Length")

View on GitHub (pinned to c245815e75)