kataras/iris · error

unexpected mime type received: %s / current implementation c

Error message

unexpected mime type received: %s / current implementation can not handle the received (and accepted) mime type: %s

What it means

This error is raised by the HTTP client's BindResponse when the server replied with a Content-Type the client cannot bind to the target struct. If the caller explicitly requested an unsupported content type it appends 'current implementation can not handle the received (and accepted) mime type', otherwise 'unexpected mime type received'. It prevents silently zero-valued unmarshalling of unexpected payloads.

Source

Thrown at x/client/client.go:541

		case *string:
			*v = string(b)
		case *[]byte:
			*v = b
		default:
			return fmt.Errorf("plain text response should accept a *string or a *[]byte")
		}

	default:
		acceptContentType := trimHeader(resp.Request.Header.Get(acceptKey))
		msg := ""
		if acceptContentType == contentType {
			// Here we make a special case, if the content type
			// was explicitly set by the request but we cannot handle it.
			msg = fmt.Sprintf("current implementation can not handle the received (and accepted) mime type: %s", contentType)
		} else {
			msg = fmt.Sprintf("unexpected mime type received: %s", contentType)
		}
		err = errors.New(msg)
	}

	return
}

func trimHeader(v string) string {
	for i, char := range v {
		if char == ' ' || char == ';' {
			return v[:i]
		}
	}
	return v
}

View on GitHub (pinned to 7bedaf55a0)

Solutions

  1. Make the server respond with the expected Content-Type (e.g. application/json) even on errors
  2. Verify the requested URL/endpoint actually returns the expected format
  3. Check and correct the request's ContentType/Accept headers before sending
  4. Inspect the raw response body/status to see what the server actually sent

Example fix

// before
req, _ := client.NewRequest(ctx, "GET", url, nil)
req.ReadJSON(&data) // unexpected mime type: text/html
// after
resp, _ := client.Do(req)
if !strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") {
    return fmt.Errorf("non-JSON response (%s): %s", resp.Header.Get("Content-Type"), string(body))
}
Defensive patterns

Strategy: validation

Validate before calling

ct := resp.Header.Get("Content-Type")
if !strings.HasPrefix(ct, "application/json") {
    return fmt.Errorf("expected JSON, got %q", ct)
}

Type guard

func isBindMimeErr(err error) bool {
    return err != nil && (strings.Contains(err.Error(), "unexpected mime type received") || strings.Contains(err.Error(), "can not handle the received (and accepted) mime type"))
}

Try / catch

err := req.ReadJSON(&out)
if err != nil && strings.Contains(err.Error(), "mime type") {
    body, _ := io.ReadAll(resp.Body)
    return fmt.Errorf("server sent non-JSON (%s): %s", resp.Header.Get("Content-Type"), body)
}

Prevention

When it happens

Trigger: client_req.ReadJSON/Bind-style calls when the response Content-Type is text/plain, HTML (error pages), XML, or any type the client has no decoder for; explicitly setting a ContentType on the request that the response body does not match.

Common situations: A reverse proxy or server returning an HTML 500 page with text/html content type while the client expects JSON; calling an endpoint that returns CSV/HTML instead of JSON; misconfigured Accept/ContentType headers.

Related errors


AI-assisted analysis of kataras/iris@7bedaf55a0 (2026-08-30). Data as JSON: /api/errors/6ba1f3da4f9e5119. Report an issue: GitHub.