OpenNHP/opennhp · error

could not parse response body

Error message

could not parse response body: %v

What it means

Raised in UploadFileToNHPServer after a 200 response was received and the body read succeeded: json.Unmarshal of the body into ServerResponse failed. It means the NHP server did not return the expected JSON envelope for the upload endpoint — the payload is truncated, HTML (e.g. a proxy error page), or a differently shaped JSON object.

Solutions

  1. Capture and log the raw bodyBytes alongside the unmarshal error to see what the server actually returned
  2. Verify the upload endpoint path and API version expected by this client match the deployed nhp-server
  3. Check whether an intermediate proxy or gateway rewrote the response (HTML error page instead of JSON)
  4. Confirm ServerResponse struct fields/tags match the server's response schema
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at endpoints/db/utils.go:281 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of OpenNHP/opennhp@6e04ca5ff0 (2026-09-07). Data as JSON: /api/errors/4ac82bec9704330f. Report an issue: GitHub.

Appendix: source

Thrown at endpoints/db/utils.go:281

	if resp.StatusCode != http.StatusOK {
		bodyBytes, _ := io.ReadAll(resp.Body)

		return "", fmt.Errorf("unexpected status code: %d, content: %s", resp.StatusCode, string(bodyBytes))
	}

	// read response body
	bodyBytes, err := io.ReadAll(resp.Body)
	if err != nil {
		return "", fmt.Errorf("could not read response body: %v", err)
	}

	// parse response body
	var respBody ServerResponse

	err = json.Unmarshal(bodyBytes, &respBody)
	if err != nil {
		return "", fmt.Errorf("could not parse response body: %v", err)
	}

	duration := time.Since(startTime)
	speed := float64(progress.TotalSize) / duration.Seconds() / (1024 * 1024)

	// change the chinese to english
	fmt.Printf("\nUpload %s to %s success! (time: %.2fs, speed: %.2fMB/s)\n",
		filePath, httpHost+respBody.FileURI, duration.Seconds(), speed)

	return httpHost + respBody.FileURI, nil
}

type UploadProgress struct {
	TotalSize   int64
	BytesRead   int64
	UploadID    string
	LastPercent int
}

View on GitHub (pinned to 6e04ca5ff0)