Получение 400 Bad Request при вызове RestAPI в eclipse, но работа в Postman

У меня есть функция, которая принимает2вход -

  1. Путь к файлу
  2. RestURL

После вызова функции она должна вернуть вывод какJSON.

Но проблема здесь, это бросание400Ошибка неверного запроса. Ниже приведен журнал ошибок, которые я получаю:

      Error Log :
URL:
http://localhost:26081/jderest/v3/orchestrator/ORC_2212160001JDE
Conn : sun.net.www.protocol.http.HttpURLConnection:http://localhost:26081/jderest/v3/orchestrator/ORC_2212160001JDE
JSON Input path : \\phxxxx155826.xxxxxx.fusionappsdphx1.xxxxxxxx.com\\FF\\Orchest\\orc1.json
{
"username": "ic8823790",
"password": "ic8823790",
"Product_No": "sss265",
"Description": "sss265",
"Stocking_Type": "s",
"G_L_Class": "in30",
"Unit_of_Measure": "ea",
"Search_Text": "sss265",
"Branch_Plant": "30"
}
HTTP CODE : 400
java.io.IOException: Server returned HTTP response code: 400 for URL: http://exx11001.ad1.xxxxxxxdephx.xxxxxxx.com:26081/jderest/v3/orchestrator/ORC_2212160001JDE
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1944)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1939)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1938)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1508)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at OrchestratorAPI.orchestratorCall(OrchestratorAPI.java:108)
at OrchestratorAPI.main(OrchestratorAPI.java:25)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: http://ems11001.ad1.fusionappsdephx.oraclevcn.com:26081/jderest/v3/orchestrator/ORC_2212160001JDE
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at OrchestratorAPI.orchestratorCall(OrchestratorAPI.java:100)
... 1 more

Это источник, который я пробовал -

      public class OrchestratorAPI {

    public static void main(String[] args) throws MalformedURLException, IOException {
        // TODO Auto-generated method stub
        String filepath = "\\\\xxxxxxx155826.xxxxxxx.fusionappsdphx1.xxxxxxx.com\\FF\\Orchest\\orc1.json";
        String orchAPI = "http://exx11001.ad1.xxxxxxxdephx.xxxxxxx.com:26081/jderest/v3/orchestrator/ORC_2212160001JDE";
        orchestratorCall(filepath, orchAPI);
    
    }
    
    public static void orchestratorCall(String filePath, String orchestAPI) throws MalformedURLException, IOException {
    
        String charset = "UTF-8";
        StringBuilder result;
        String content = "";
        String path = "", sid = "";
        String paramsJSON="";
    
        URLConnection con = new URL(orchestAPI).openConnection();
        System.out.println("URL : "+orchestAPI + "\nConn : "+con);
    
        System.out.println("JSON Input path : " + filePath);
        try {
            paramsJSON = new String(
                Files.readAllBytes(Paths.get(filePath)));
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    
        System.out.println(paramsJSON);
    
        try {
            ((HttpURLConnection) con).setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "application/json; UTF-8");
            con.setRequestProperty("Accept", "application/json");
    
            con.setDoOutput(true);
            con.setReadTimeout(60000);
            con.setConnectTimeout(60000);
    
            try (OutputStream os = con.getOutputStream()) {
                byte[] input = paramsJSON.getBytes(charset);
                os.write(input, 0, input.length);
            }
    
            int code = ((HttpURLConnection) con).getResponseCode();
            System.out.println("HTTP CODE : " + String.valueOf(code));
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        try {
            // Receive the response from the server
            InputStream in = new BufferedInputStream(con.getInputStream());
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            result = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }
    
            System.out.println("JSON Parser -> result: " + result.toString());
    
            try {
                // Create new report file
                content = result.toString();
                path = "\\\\xxxxxxx155826.xxxxxxx.fusionappsdphx1.xxxxxxxx.com\\FF\\Orchest\\" + "" + "OrchestratorReport.json";
                System.out.println("Home Dir path : " + path);
                File file = new File(path);
    
                // If file doesn't exists, then create it
                if (!file.exists()) {
                    file.createNewFile();
                }
    
                FileWriter fw = new FileWriter(file.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
    
                // Write in file
                bw.write(content);
    
                // Close connection
                bw.close();
            } catch (Exception e) {
                System.out.println(e);
            }
    
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        ((HttpURLConnection) con).disconnect();   
    }
}

1 ответ

проблема решена. Я только что удалилUTF-8изsetRequestPropertyсоединения, и теперь оно работает.

До :

      con.setRequestProperty("Content-Type", "application/json; UTF-8");

После :

      con.setRequestProperty("Content-Type", "application/json");
Другие вопросы по тегам