{"record":{"id":"6564118bd88d0ef5","repo":"ory/hydra","slug":"failed-to-encode-request-body-s","errorCode":null,"errorMessage":"Failed to encode request body: %s","messagePattern":"Failed to encode request body: (.+?)","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"cmd/cmd_perform_device_flow.go","lineNumber":201,"sourceCode":"\tif challenge == \"\" {\n\t\thttp.Error(w, \"device_challenge is required\", http.StatusBadRequest)\n\t\treturn\n\t}\n\n\t// Accept the user code with a hand-rolled request instead of the generated\n\t// client: other modules in this repository compile this package against the\n\t// released hydra-client-go/v2 module, which predates the device\n\t// authorization API.\n\tcfg := s.cl.GetConfig()\n\tif len(cfg.Servers) == 0 {\n\t\thttp.Error(w, \"No Hydra endpoint is configured\", http.StatusInternalServerError)\n\t\treturn\n\t}\n\tacceptURL := strings.TrimSuffix(cfg.Servers[0].URL, \"/\") +\n\t\t\"/admin/oauth2/auth/requests/device/accept?device_challenge=\" + url.QueryEscape(challenge)\n\tbody, err := json.Marshal(map[string]string{\"user_code\": userCode})\n\tif err != nil {\n\t\thttp.Error(w, fmt.Sprintf(\"Failed to encode request body: %s\", err), http.StatusInternalServerError)\n\t\treturn\n\t}\n\treq, err := http.NewRequestWithContext(r.Context(), http.MethodPut, acceptURL, bytes.NewReader(body))\n\tif err != nil {\n\t\thttp.Error(w, fmt.Sprintf(\"Failed to create request: %s\", err), http.StatusInternalServerError)\n\t\treturn\n\t}\n\treq.Header.Set(\"Content-Type\", \"application/json\")\n\treq.Header.Set(\"Accept\", \"application/json\")\n\n\thc := cfg.HTTPClient\n\tif hc == nil {\n\t\thc = http.DefaultClient\n\t}\n\tres, err := hc.Do(req)\n\tif err != nil {\n\t\thttp.Error(w, fmt.Sprintf(\"Failed to accept user code request: %s\", err), http.StatusInternalServerError)\n\t\treturn","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/ory/hydra/blob/4174065ffb052799890f7480f5360a877a67ffc1/cmd/cmd_perform_device_flow.go#L183-L219","documentation":"This error is returned by the POSTdevice handler in the CLI device-flow helper when json.Marshal fails to serialize the {\"user_code\": ...} payload that is PUT to the Hydra admin device-accept endpoint. json.Marshal on a map[string]string practically never fails (no channels, funcs, or unsupported types), so hitting this indicates a programming error rather than a runtime condition. It is surfaced to the client as a plain-text 500 via http.Error.","triggerScenarios":"json.Marshal(map[string]string{\"user_code\": userCode}) returns an error at cmd/cmd_perform_device_flow.go:201. With the current literal map of strings this cannot happen at runtime; it would only fail if the payload type were changed to include unsupported values (channels, funcs, cyclic structures) or a custom MarshalJSON returning an error.","commonSituations":"Practically never seen in production with this exact code. Developers refactoring the handler to send structured payloads (e.g. embedding a struct with unmarshalable fields, or passing context through the map) can introduce it. Custom user_code wrapper types with faulty MarshalJSON methods are the realistic path.","solutions":["Verify the marshaled value is only plain strings; if it must hold other types, use a typed struct with JSON tags instead of map[string]string.","Log the underlying marshal error (it already appears in the 500 body) and inspect the value being marshaled.","Keep the http.Error fallback so failures return 500 rather than panicking; no client-side fix is possible."],"exampleFix":"// before\nbody, err := json.Marshal(map[string]string{\"user_code\": userCode})\n// after\ntype acceptReq struct {\n\tUserCode string `json:\"user_code\"`\n}\nbody, err := json.Marshal(acceptReq{UserCode: userCode})","handlingStrategy":"validation","validationCode":"payload := map[string]string{\"user_code\": userCode}\nif _, err := json.Marshal(payload); err != nil {\n\treturn fmt.Errorf(\"payload unserializable: %w\", err)\n}","typeGuard":"func marshalable(v any) bool {\n\t_, err := json.Marshal(v)\n\treturn err == nil\n}","tryCatchPattern":"body, err := json.Marshal(payload)\nif err != nil {\n\tlog.Printf(\"marshal failed: %v\", err)\n\thttp.Error(w, \"internal error\", http.StatusInternalServerError)\n\treturn\n}","preventionTips":["Keep request payloads as plain string maps or tagged structs — never channels, funcs, or cyclic refs","Write a unit test that marshals every request payload type","Prefer structs with json tags over map[string]string for evolving payloads"],"tags":["http","json","handler"],"backgroundTag":"json-marshal-failed","analyzedSha":"4174065ffb052799890f7480f5360a877a67ffc1","analyzedAt":"2026-09-03T14:52:41.581Z","contentChangedAt":"2026-09-03T14:52:41.581Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}