-2

Im using triying to connect my projecto to a Neo4j DB and I can do it when I do a GET request, and using the same code (changing GETMethod for POSTMethod) I can´t do it.

It´s my code:

String name = "xxx"; String password = "xxx";

    String authString = name + ":" + password;

    byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());

    String authStringEnc = new String(authEncBytes);

    HttpMethod method = null;
    HttpClient client = new HttpClient();       
    //String url = "http://localhost:7474/db/data/relationship";
    String url = "http://localhost:7474/db/data/cypher";


    method = new PostMethod( String.format(url));
    method.addRequestHeader(new Header("Content-Type", "application/json; charset=UTF-8"));
    method.addRequestHeader("Authorization", "Basic "+ authStringEnc);
    method.setQueryString("{\"query\" : \"MATCH (x{id:'123'})-[r]->(n) RETURN n.name, r.status\",\"params\" : {  }}");

    try {

        client.executeMethod(method);

But there is a HttpException.

Is my first time with Rest and Neo4j.

Someone can help me?

Thanks.

Silvia
  • 1
  • 1
  • 1
    _"But there is a HttpException"_ -- well, are you going to share the exception and stack trace with us? Do you expect us to guess what the problem is? Also tell us which statement in your code throws the exception. – Jim Garrison Aug 10 '16 at 16:57
  • I could solve it, thank you. – Silvia Aug 12 '16 at 00:03

1 Answers1

0

Answer goes there: HTTP POST using JSON in Java

try using DefaultHttpClient and if that doesn't resolve the problem also try using HttpPost instead of HttpMethod.

HttpClient client = new DefaultHttpClient();       
String url = "http://localhost:7474/db/data/cypher";

HttpPost method = new HttpPost(url);

//You can use StringEntity for your query
StringEntity params= new StringEntity("your query goes here", ContentType.APPLICATION_JSON);
method.setEntity(params);

//for the response use followings
HttpResponse response = client.execute(method);
HttpEntity entity = response.getEntity();
Community
  • 1
  • 1
trd3v3lop
  • 323
  • 2
  • 15