{"record":{"id":"b84767c1abbf17d7","repo":"SonarSource/sonarqube","slug":"unable-to-contact-bitbucket-server-got-an-unexpec","errorCode":null,"errorMessage":"Unable to contact Bitbucket server, got an unexpected response","messagePattern":"Unable to contact Bitbucket server, got an unexpected response","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java","lineNumber":168,"sourceCode":"  private String getBodyString(Request request) {\n    try (Response response = client.newCall(request).execute()) {\n      String bodyString = response.body().string();\n      validateResponseBody(response.isSuccessful(), bodyString);\n      handleHttpErrorIfAny(response.isSuccessful(), response.code(), bodyString);\n      return bodyString;\n    } catch (IOException e) {\n      LOG.info(UNABLE_TO_CONTACT_BITBUCKET_SERVER + \": {}\", e.getMessage(), e);\n      throw new IllegalArgumentException(UNABLE_TO_CONTACT_BITBUCKET_SERVER, e);\n    }\n  }\n\n  protected static <G> G applyHandler(Function<String, G> handler, String bodyString) {\n    try {\n      return handler.apply(bodyString);\n    } catch (JsonSyntaxException e) {\n      LOG.info(UNABLE_TO_CONTACT_BITBUCKET_SERVER + \". Unexpected body response was : [{}]\", bodyString);\n      LOG.info(UNABLE_TO_CONTACT_BITBUCKET_SERVER + \": {}\", e.getMessage(), e);\n      throw new IllegalArgumentException(UNABLE_TO_CONTACT_BITBUCKET_SERVER + \", got an unexpected response\", e);\n    }\n  }\n\n  protected static void validateResponseBody(boolean isSuccessful, String bodyString) {\n    if (isSuccessful) {\n      try {\n        buildGson().fromJson(bodyString, Object.class);\n      } catch (JsonParseException e) {\n        LOG.info(UNEXPECTED_RESPONSE_FROM_BITBUCKET_SERVER + \" : [{}]\", bodyString);\n        throw new IllegalArgumentException(UNEXPECTED_RESPONSE_FROM_BITBUCKET_SERVER, e);\n      }\n    }\n  }\n\n  protected static void handleHttpErrorIfAny(boolean isSuccessful, int httpCode, String bodyString) {\n    if (!isSuccessful) {\n      String errorMessage = getErrorMessage(bodyString);\n      LOG.info(UNABLE_TO_CONTACT_BITBUCKET_SERVER + \": {} {}\", httpCode, errorMessage);","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java#L150-L186","documentation":"applyHandler runs the caller-provided Gson deserialization handler on the response body. If the handler throws JsonSyntaxException, the client concludes the body, while reachable, is not the expected Bitbucket JSON shape, logs the body, and throws IllegalArgumentException('Unable to contact Bitbucket server, got an unexpected response').","triggerScenarios":"A successful HTTP response whose body fails strict Gson parsing of the target model, e.g. a Bitbucket proxy/login HTML page, a CDN/WAF error page, or a Bitbucket Data Center version returning a JSON shape the handler's target class (e.g. BranchesList) cannot parse.","commonSituations":"Requests intercepted by an SSO/login redirect returning HTML with status 200; a reverse proxy or antivirus appliance injecting content; Bitbucket Server version mismatch changing the REST API payload; hitting a non-Bitbucket service on the configured URL.","solutions":["Open the server log and inspect 'Unexpected body response was : [...]' to see the actual payload.","If the body is HTML/login page, the URL is hitting SSO or a wrong service — fix the serverUrl or exempt SonarQube from SSO redirects.","If the body is valid but differently-shaped JSON, check the Bitbucket Server version's REST API and upgrade the SonarQube ALM plugin/version.","Confirm the configured URL points at the Bitbucket Server base URL, not a proxy path that rewrites responses."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  // call client\n} catch (IllegalArgumentException e) {\n  if (e.getMessage() != null && e.getMessage().endsWith(\"got an unexpected response\")) {\n    // body was non-JSON/unexpected: inspect logged body, fall back or surface a config warning\n  }\n}","preventionTips":["Check the server log for 'Unexpected body response was' to identify SSO/proxy interference.","Exempt SonarQube's source IPs/agents from SSO redirects on Bitbucket.","Keep the SonarQube Bitbucket integration and Bitbucket Server versions compatible.","Point the ALM setting at the Bitbucket base URL, not a proxy path that rewrites responses."],"tags":["java","bitbucket-server","gson","unexpected-response"],"backgroundTag":"unexpected-response-shape","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}