Akin Dönmez
Akin Dönmez

Reputation: 363

why am i getting http error code 500 when i am sending a rest request

when i put the url on the browser, i can get the json

http://api.openweathermap.org/data/2.5/weather?q=Rome, Italy&appid=2de143494c0b295cca9337e1e96b00e0

but when i do it through java with a http get request i m getting http error code 500. Any ideas why ?

private String getWeather(Booking booking){
        StringBuilder sb = new StringBuilder();
        URL url;
        try {
            url = new URL("http://api.openweathermap.org/data/2.5/weather?q="+booking.getDestination()+"&appid=2de143494c0b295cca9337e1e96b00e0");
            System.out.println(url);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            }

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

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }
            conn.disconnect();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        return sb.toString();
    }

Upvotes: 2

Views: 2311

Answers (1)

RealSkeptic
RealSkeptic

Reputation: 34638

When you create a URL or a URI, there are rules about which characters are allowed in each part of it. A space is not allowed. Other characters (like /, & etc.) have particular meaning in the URL so they can't exist in the parameters themselves. The standard for creating a URI is in RFC 3986.

Such characters that are not allowed in the URI are escaped by replacing them with a sequence like %20, %3F and so on.

When you paste a URL that has an illegal character like a space into a browser location field, modern browsers usually correct it automatically. So they will replace the space with %20 or + (using a + for a space is an older standard, still used in web forms). The correction is not perfect. Most browsers won't be able to properly fix a parameter that includes an &, for example. So if you tried Sarajevo, Bosnia&Herzegovina instead of Rome, Italy, the browser may interpret Herzegovina as the name of a separate, empty parameter.

But in any case, the parameter must be escaped when it is included in a URI. The most basic method to use is:

url = new URL("http://api.openweathermap.org/data/2.5/weather?q="
               + URLEncoder.encode(booking.getDestination(), "UTF-8")
               + "&appid=2de143494c0b295cca9337e1e96b00e0");

This may work in many cases - but it uses the old standard I was talking about, the one that replaces a space with a +. That standard has some additional differences from the RFC 3986 standard character escaping. Many web servers are not fussy about these differences and will interpret the value correctly.

But if the REST server you are working with is fussy about standard compliance, you should use real URI escaping. There is no method for this that I'm aware of in the Java standard edition, but:

  • If you are using a J2EE compliant platform, you may use the javax.ws.rs.core.UriBuilder class to build your URI.
  • If you are using Spring, it has a UriUtils class.
  • Guava has a PercentEscaper class.
  • etc.

Upvotes: 2

Related Questions