{"record":{"id":"420cd5ea76910dff","repo":"kataras/iris","slug":"plain-text-response-should-accept-a-string-or-a","errorCode":null,"errorMessage":"plain text response should accept a *string or a *[]byte","messagePattern":"plain text response should accept a \\*string or a \\*\\[\\]byte","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"x/client/client.go","lineNumber":528,"sourceCode":"// the response headers and the dest is a *string.\nfunc BindResponse(resp *http.Response, dest any) (err error) {\n\tcontentType := trimHeader(resp.Header.Get(contentTypeKey))\n\tswitch contentType {\n\tcase contentTypeJSON: // the most common scenario on successful responses.\n\t\treturn json.NewDecoder(resp.Body).Decode(&dest)\n\tcase contentTypePlainText:\n\t\tb, err := io.ReadAll(resp.Body)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tswitch v := dest.(type) {\n\t\tcase *string:\n\t\t\t*v = string(b)\n\t\tcase *[]byte:\n\t\t\t*v = b\n\t\tdefault:\n\t\t\treturn fmt.Errorf(\"plain text response should accept a *string or a *[]byte\")\n\t\t}\n\n\tdefault:\n\t\tacceptContentType := trimHeader(resp.Request.Header.Get(acceptKey))\n\t\tmsg := \"\"\n\t\tif acceptContentType == contentType {\n\t\t\t// Here we make a special case, if the content type\n\t\t\t// was explicitly set by the request but we cannot handle it.\n\t\t\tmsg = fmt.Sprintf(\"current implementation can not handle the received (and accepted) mime type: %s\", contentType)\n\t\t} else {\n\t\t\tmsg = fmt.Sprintf(\"unexpected mime type received: %s\", contentType)\n\t\t}\n\t\terr = errors.New(msg)\n\t}\n\n\treturn\n}\n","sourceCodeStart":510,"sourceCodeEnd":546,"githubUrl":"https://github.com/kataras/iris/blob/7bedaf55a0b64bbb2248a5845a2c60d81a30996a/x/client/client.go#L510-L546","documentation":"BindResponse dispatches on the response's Content-Type header; when it is plain text it only binds the body into *string or *[]byte. If dest is any other type while the server answered with text/plain, it returns this error. The library is deliberately strict so mismatches between the declared response content type and the destination are caught early.","triggerScenarios":"Calling client.BindResponse(resp, dest) where resp.Header.Get(\"Content-Type\") is \"text/plain\" (compare uses trimHeader, so parameters like charset are trimmed) but dest is e.g. *int, *map[string]any, or a struct pointer — unlike ReadPlain, BindResponse's plain-text branch does not support *int.","commonSituations":"Reusing one BindResponse call site for responses that may be JSON or plain text and passing an *int (works only if you used ReadPlain, not BindResponse); a server suddenly responding text/plain to an endpoint the client expected JSON from; passing non-pointer values by mistake.","solutions":["Pass a *string or *[]byte as dest when the response is plain text, then parse (e.g. strconv.Atoi) as needed.","Check resp.Content-Type before binding and choose the right dest type per content type.","If the body should be JSON, fix the server or set/verify the Accept header so the server replies with application/json.","Use Client.ReadPlain instead if you need *int binding of a plain-text body."],"exampleFix":"// before\nvar n int\nerr := client.BindResponse(resp, &n) // text/plain body\n\n// after\nvar s string\nif err := client.BindResponse(resp, &s); err != nil { return err }\nn, err := strconv.Atoi(strings.TrimSpace(s))","handlingStrategy":"type-guard","validationCode":"func bindDestOK(resp *http.Response, dest any) bool {\n    ct := resp.Header.Get(\"Content-Type\")\n    if i := strings.IndexAny(ct, \" ;\"); i >= 0 { ct = ct[:i] }\n    if ct == \"plain/text\" {\n        switch dest.(type) {\n        case *string, *[]byte:\n            return true\n        default:\n            return false\n        }\n    }\n    return true\n}","typeGuard":"func isPlainTextBindDest(dest any) bool {\n    switch dest.(type) {\n    case *string, *[]byte:\n        return true\n    }\n    return false\n}","tryCatchPattern":"if err := client.BindResponse(resp, dest); err != nil {\n    if strings.Contains(err.Error(), \"plain text response should accept\") {\n        // rebind into a *string and convert manually\n        var s string\n        return client.BindResponse(resp, &s)\n    }\n    return err\n}","preventionTips":["Match dest type to the response Content-Type: text/plain => *string or *[]byte only (no *int here).","Inspect Content-Type before calling BindResponse when the endpoint can return mixed types.","Remember BindResponse is stricter than ReadPlain: convert ints from string yourself.","Keep one typed helper per endpoint so dest types stay consistent."],"tags":["go","http-client","content-type","type-mismatch"],"backgroundTag":"content-type-destination-mismatch","analyzedSha":"7bedaf55a0b64bbb2248a5845a2c60d81a30996a","analyzedAt":"2026-08-30T20:38:16.250Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}