2

I have a function which takes 2 input -

  1. JSON filepath
  2. RestURL

After calling the function, it should return the output as JSON.

But the problem here, it is throwing 400 Bad request error. Below is the log of error which i am getting -

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 

This is source what i have tried -

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(); } } 
2
  • The client code looks ok to me. What matters is the backend. Do you have the code for that? You said it's working with postman, how are you sending the request there? POST? Which headers? How are you sending the request body? Commented Feb 2, 2023 at 8:50
  • I am using POST request to call the API. Request body is a raw JSON like this- { "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" } @invzbl3 Commented Feb 2, 2023 at 9:28

1 Answer 1

0

the issue is solved. I have just removed the UTF-8 from the setRequestProperty of the connection and it is working now.

Before :

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

After :

con.setRequestProperty("Content-Type", "application/json"); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.