{"record":{"id":"6e1bfe17becd999a","repo":"apache/hadoop","slug":"unexpected-http-response-code-code-op-get","errorCode":null,"errorMessage":"Unexpected HTTP response: code=${code} != ${op.getExpectedHttpResponseCode()}, ${op.toQueryString()}, message=${conn.getResponseMessage()}","messagePattern":"Unexpected HTTP response: code=(.+?) != (.+?), (.+?), message=(.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/WebHdfsFileSystem.java","lineNumber":524,"sourceCode":"      in.close();\n    }\n  }\n\n  private static Map<?, ?> validateResponse(final HttpOpParam.Op op,\n      final HttpURLConnection conn, boolean unwrapException)\n      throws IOException {\n    final int code = conn.getResponseCode();\n    // server is demanding an authentication we don't support\n    if (code == HttpURLConnection.HTTP_UNAUTHORIZED) {\n      // match hdfs/rpc exception\n      throw new AccessControlException(conn.getResponseMessage());\n    }\n    if (code != op.getExpectedHttpResponseCode()) {\n      final Map<?, ?> m;\n      try {\n        m = jsonParse(conn, true);\n      } catch(Exception e) {\n        throw new IOException(\"Unexpected HTTP response: code=\" + code + \" != \"\n            + op.getExpectedHttpResponseCode() + \", \" + op.toQueryString()\n            + \", message=\" + conn.getResponseMessage(), e);\n      }\n\n      if (m == null) {\n        throw new IOException(\"Unexpected HTTP response: code=\" + code + \" != \"\n            + op.getExpectedHttpResponseCode() + \", \" + op.toQueryString()\n            + \", message=\" + conn.getResponseMessage());\n      } else if (m.get(RemoteException.class.getSimpleName()) == null) {\n        return m;\n      }\n\n      IOException re = JsonUtilClient.toRemoteException(m);\n\n      //check if exception is due to communication with a Standby name node\n      if (re.getMessage() != null && re.getMessage().endsWith(\n          StandbyException.class.getSimpleName())) {\n        LOG.trace(\"Detected StandbyException\", re);","sourceCodeStart":506,"sourceCodeEnd":542,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/WebHdfsFileSystem.java#L506-L542","documentation":"validateResponse checks every WebHDFS operation's HTTP status against the expected code. When the status differs and the error body cannot be parsed as JSON, it throws this IOException containing the actual code, expected code, operation query string, and HTTP message. The JSON parse failure is attached as the cause, which usually identifies an HTML/plain-text error body or an unavailable stream.","triggerScenarios":"A WebHDFS operation receives an unexpected HTTP status and jsonParse(conn, true) throws while reading its error stream. Examples are a proxy returning an HTML 502 page, a server error page, a request routed to a non-WebHDFS service, or a truncated error response. HTTP 401 is handled separately as AccessControlException.","commonSituations":"NameNode or HttpFS outage behind a gateway; wrong URL or port; firewall/proxy 5xx pages; CSRF or authentication filters issuing non-JSON responses; disk, memory, or handler failures on the server.","solutions":["Replay the operation with curl -i using the same URL, query string, and credentials, and inspect the status plus body shown in the exception.","Check NameNode/HttpFS and proxy logs for the corresponding request to find the real server-side cause.","Correct endpoint URLs, ports, authentication, and CSRF configuration if the response body shows another service or filter answered.","Retry only after confirming a transient server condition, because configuration and routing failures will reproduce identically."],"exampleFix":"# before: exception shows code=502 != 200, op=OPEN\n# typical reverse-proxy HTML error is the cause\n\n# after: inspect and bypass the failing hop\ncurl -i --negotiate -u : \"http://nn:9870/webhdfs/v1/user/alice/f?op=OPEN&offset=0&length=1024\"\n# then configure the client to call nn:9870 directly rather than the proxy returning 502","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  return fs.open(path);\n} catch (IOException e) {\n  String message = String.valueOf(e.getMessage());\n  if (message.startsWith(\"Unexpected HTTP response: code=\")) {\n    // Configuration/routing failures are not retryable; transient 5xx from an active NN may be.\n    LOG.error(\"WebHDFS returned an unexpected non-JSON response: {}\", message, e);\n  }\n  throw e;\n}","preventionTips":["Monitor unexpected HTTP response codes separately from HDFS RemoteExceptions.","Keep reverse-proxy error bodies from masking WebHDFS JSON errors.","Verify authentication and CSRF headers for every WebHDFS-enabled deployment."],"tags":["java","hadoop","webhdfs","http","status-code","json"],"backgroundTag":"http-status-code-mismatch","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}