0

I'm getting a 'Server returned HTTP response code: 500' error although I have checked what I'm sending (I even tried sending it with an online tool and it worked). The API Key and the JSON are correct. I get this error when trying to read the input stream with 'connection.getInputStream()'. Where could this be comming frome ? Did I forget something ? I am trying to implement this feature from the openrouteservice API : https://openrouteservice.org/dev/#/api-docs/v2/directions/{profile}/post

 public static UPSRoute getRoute(Location start, Location end, String language) { if (language.equals("fr")) { JSONObject jsonObject = null; try { URL url = new URL("https://api.openrouteservice.org/v2/directions/foot-walking"); String payload = "{\"coordinates\":[[" + start.getCoordinates() + "],[" + end.getCoordinates() + "]],\"language\":\"fr\"}"; System.out.println(payload); //{"coordinates":[[1.463478,43.562038],[1.471717,43.560787]],"language":"fr"} byte[] postData = payload.getBytes(StandardCharsets.UTF_8); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Authorization", API_KEY); connection.setRequestProperty("Accept", "application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8"); connection.setDoOutput(true); try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) { wr.write(postData); } BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); // Error is right here String inputLine; StringBuffer content = new StringBuffer(); while ((inputLine = in.readLine()) != null) { content.append(inputLine); } in.close(); connection.disconnect(); jsonObject = new JSONObject(content.toString()); } catch (IOException | JSONException e) { e.printStackTrace(); } return new UPSRoute(jsonObject); } else { return getRoute(start, end); } } 

Here is the error :

java.io.IOException: Server returned HTTP response code: 500 for URL: https://api.openrouteservice.org/v2/directions/foot-walking/json at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1913) at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1509) at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:245) at UPSRouteService.getRoute(UPSRouteService.java:63) at Main.main(Main.java:5) 
6
  • 4
    Use any rest client like Apache Http-Client, Jersey client instead of using URLCOnnection. Besides, post the structure of your json you are sending to the api call. Commented Jun 5, 2019 at 15:03
  • Look at the payload of the 500 response, it might actually explain what went wrong. Commented Jun 5, 2019 at 15:31
  • Why are you wrapping with DataOutputStream, when you don't use any of its methods? Commented Jun 5, 2019 at 15:33
  • 1
    The server is likely rejecting the POST because you didn't set Content-Type header. Commented Jun 5, 2019 at 15:37
  • img/png; charset=utf-8 ?!?!? --- Shouldn't that be image/png, with image spelled out, and without the charset given that a PNG image is binary data? Specifying charset makes no sense for binary data. Commented Jun 5, 2019 at 15:40

1 Answer 1

1

Thanks to Andreas, it was just missing the line :

connection.setRequestProperty("Content-Type", "application/json"); 

It works fine now.

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.