0

I am trying to read the response body for an error response(404,400,500) using java application and basic http client. When testing in my local , I am able to get the proper json response. But when I deployed to azure I am getting the error response body in HTML tags.

Error response I got when deployed to azure and testing from there:

400 Error400 error while attempting to access resource

@AddLoggerInAppInsights public Map exchangeNativeHttpRequest(String url, String jsonPayLoad, String correlationId) {

    Map<String,Object> responseMap = null;
    try {

        URL httpUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty(CLIENT_ID_KEY, System.getenv(CLIENT_ID_VALUE));
        conn.setRequestProperty(CLIENT_SECRET_KEY, System.getenv(CLIENT_SECRET_VALUE));
        conn.setRequestProperty(CORRELATION_ID, correlationId);

        OutputStream os = conn.getOutputStream();
        os.write(jsonPayLoad.getBytes());
        os.flush();
        HttpStatus httpStatus = HttpStatus.valueOf(conn.getResponseCode());
        BufferedReader br = null;
        if (conn.getResponseCode() == org.apache.http.HttpStatus.SC_OK) {
             br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));
        }else if (conn.getResponseCode() == org.apache.http.HttpStatus.SC_BAD_REQUEST){
             br = new BufferedReader(new InputStreamReader(
                    (conn.getErrorStream())));
        }else {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }
        String output;
        StringBuilder sb = new StringBuilder();
        while ((output = br.readLine()) != null) {
            sb.append(output);
        }
        telemetryClient.trackEvent(correlationId+":" +"Response for Create Breakdown IBMI API from HTTP_CONNECTION: "+sb.toString());
         responseMap = new HashMap<>();
         responseMap.put("statusCode",httpStatus);
         responseMap.put("response", sb.toString());
        conn.disconnect();

      } catch (MalformedURLException e) {

          telemetryClient.trackException(e);

      } catch (IOException e) {

          telemetryClient.trackException(e);

     }
    telemetryClient.trackEvent(correlationId+":" +"Returned Response of Create Breakdown IBMI API from HTTP_CLIENT: "+responseMap.toString());
    return responseMap;

}
sravan
  • 1
  • 1
  • I am getting this html response : 400 Error400 error while attempting to access resource

    – sravan Nov 18 '19 at 21:35
  • The actual response coming from api, when testing in my local or postman is this :{ "errorMessages": [ { "errorMessage": "City not valid" } ] } – sravan Nov 18 '19 at 21:37
  • seems that the error you're getting is actually the app services one, not the one you're looking for. Can you post some code? – Thiago Custodio Nov 18 '19 at 22:51
  • Seems like it is not a code issue. I have tried using different rest templates including the basic http connection one. Everything works fine in local, but when deployed to azure for some reason the actual response is overrided by the html response which I placed in previous comment. Handled 4XX/5XX similiar to this url: https://stackoverflow.com/questions/16194014/spring-mvc-resttemplate-launch-exception-when-http-404-happens and tried other REST API and even JAVA native http client too – sravan Nov 18 '19 at 23:04
  • Have you tried with any other urls? By the way, what azure service did you use? Web app or VM? – Jack Jia Nov 19 '19 at 01:39
  • I have tried other external apis, they are working fine. I am using app services in azure. – sravan Nov 19 '19 at 16:34

0 Answers0