简体   繁体   中英

How to get the response json on HTTP error 400 in Java

I'm making a request using HttpUrlConnection (POST):

try
    {

        String v_Authorization="";
        String v_MessageResponseCode="";

        switch (in_TipoAuthorization) {

            case 1: //No Auth  
                v_Authorization = in_Authorization;
                break;

            case 2: //Basic Auth
                //...
                break;

            case 3: //Bearer Token
                //...
                break;

            case 4: //OAuth2 Bearer Token
                //...
                break;

        }

        URL url = new URL(in_URL);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

        conn.setRequestProperty("Authorization", v_Authorization);
        conn.setRequestProperty("Content-Type", in_ContentType);
        conn.setRequestProperty("Aceept", in_Accept);       
        conn.setRequestMethod(in_Method);
        conn.setDoOutput(true);
        conn.setDoInput(true);

        OutputStream os = conn.getOutputStream();
        os.write(in_JsonFile.getBytes("UTF-8"));
        os.flush();    
        os.close();

        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
        StringBuilder builderRetorno = new StringBuilder();

        String retorno="";
        while ((retorno = br.readLine()) != null) {
            if (in_ImportCsvFile == 1) {
                builderRetorno.append(retorno + "\n");
            } else {
                builderRetorno.append(retorno);
            }
        }

        out_HTTP = builderRetorno.toString();
        out_getResponseCode = conn.getResponseCode();


      } catch(MalformedURLException ex){
            logInfo(ex.toString());
            ex.printStackTrace();
            out_HTTP = ex.toString();

        } catch(IOException e) {
            logInfo(e.toString());
            e.printStackTrace();
            out_HTTP = e.toString();

      } finally {
          conn.disconnect();
    }

When an error occurs I only have a Bad Request 400 return and e.printStackTrace does not tell me exactly what the error is:

    java.io.IOException: Server returned HTTP response code: 400 for URL: https://xxxx.com.br/v1/account/b48ec133-283b-4de2-a7e3-9d4b7517148a/json/position
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:83)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:57)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:437)
    at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1956)
    at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1951)
    at java.security.AccessController.doPrivileged(AccessController.java:703)
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1950)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1520)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1504)
    at com.ibm.net.ssl.www2.protocol.https.b.getInputStream(b.java:91)
    at com.informatica.powercenter.server.jtx.JTXPartitionDriverImplGen.execute(JTXPartitionDriverImplGen.java:1504)
Caused by: 
java.io.IOException: Server returned HTTP response code: 400 for URL: https://xxxx.com.br/v1/account/b48ec133-283b-4de2-a7e3-9d4b7517148a/json/position
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1906)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1504)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:491)
    at com.ibm.net.ssl.www2.protocol.https.b.getResponseCode(b.java:56)
    at com.informatica.powercenter.server.jtx.JTXPartitionDriverImplGen.execute(JTXPartitionDriverImplGen.java:1493)

However, when I make the same request via POSTMAN, it returns exactly what the error is, example:

{ "profile": { "email": "must be a valid email address" } }

enter image description here

How do I get this same error information in my java code?

Thanks.

Recommend you use ok-http .

Here is some helpful link.

http post request (json data)

get an error message from response

If you only want to use HttpsURLConnection .Like @Smile says, use conn.getErrorStream()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM