1
votes

I am getting below exception while running cypher using HTTPConnection.

Server returned HTTP response code: 500 for URL: http://localhost:7474/db/data/cypher

public class HTTPConnectionTest {
    public static void main(String[] args) throws Exception {
        try {
                System.out.println("Testing HTTPConnection");
                StringBuffer responseString = new StringBuffer();
                String url = "http://localhost:7474/db/data/cypher";

                //String query = "match (user:USER{id:\'Sree\'}) return user ";
                String query = "match (user:USER{id:\"Sree\"}) return user ";
                URL neo4jUrl = new URL(url);
                HttpURLConnection httpConn = (HttpURLConnection) neo4jUrl
                        .openConnection();
                httpConn.setRequestMethod("POST");
                httpConn.setDoOutput(true);
                httpConn.setDoInput(true);
                httpConn.setRequestProperty("Content-Type", "application/json");
                String urlParameters = "{\"query\":\"" + query + "\"}";
                httpConn.setRequestProperty("Accept",
                        "application/json; charset=UTF-8");

                DataOutputStream wr = new DataOutputStream(
                        httpConn.getOutputStream());
                wr.writeBytes(urlParameters);
                wr.flush();
                wr.close();

                BufferedReader in = new BufferedReader(new InputStreamReader(
                        httpConn.getInputStream()));
                String inputLine;

                while ((inputLine = in.readLine()) != null) {
                    responseString.append(inputLine);
                }
                System.out.println("Out put " + responseString);
                in.close();
            } catch (Exception e) {
                System.out.println("Exception" + e.getMessage());
            }
    }
}

If I pass value in cypher with single quotes it is working and getting output object.

String query = "match (user:USER{id:\'Sree\'}) return user ";

Any suggestions? Thank you in advance!

1

1 Answers

1
votes

This looks silly to me.

a small change in code. Converted params into JSONObject string

JSONObject jsonObject = new JSONObject();
jsonObject.put("query", query);
String urlParameters = jsonObject.toString();

instead of

String urlParameters = "{\"query\":\"" + query + "\"}";