CMDBuild Forum

REST API For Search

Dear Team,

I was trying for search using filter [code has been attached here]

public void CmdbuildSerachMethod1(String SessionId) {
        String SearchJson = "{\"filter\": {\"attribute\":\"Model\",\"operator\":\"contain\",\"value\":[\"3900\"],\"parameterType\":\"fixed\" }}";

        String ClassName = "Router";
        System.out.println(SearchJson);
        URL url;
        HttpURLConnection connection = null;

        try {

            JSONParser parser = new JSONParser();
            JSONObject json = (JSONObject) parser.parse(SearchJson);

            String targetURL = "http://localhost:8080/cmdbuild/services/rest/v2/classes/"+ClassName+"/cards?CMDBuild-Authorization="
                    + SessionId;
            String urlParameters = json.toJSONString();

            url = new URL(targetURL);
            System.out.println(url);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type", "application/json");

            connection.setRequestProperty("Content-Length",
                    "" + Integer.toString(urlParameters.getBytes().length));
            connection.setRequestProperty("Content-Language", "en-US");

            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            // Send request
            DataOutputStream wr = new DataOutputStream(
                    connection.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();
            // Get Response

            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            // System.out.println("9"+rd.readLine());

            if (rd != null) {
                System.out.println(rd.readLine());
            }
            rd.close();

        } catch (Exception e) {
            System.out.println(e);
        }

    }

 

When I call this function, a new blank record has been added in to the Router class.

 

 

Hello,
 
First, you need to call "/sessions" with username and password in HTTP Post body (in json format) to get authorization code. Then you can call other service with authorization code in header.
 
You might want to try free tools SOAP-UI for testing.
 
Regards,
Herru
 
 
Previously Sreekumar wrote:

Dear Team,

I was trying for search using filter [code has been attached here]

public void CmdbuildSerachMethod1(String SessionId) {
        String SearchJson = "{\"filter\": {\"attribute\":\"Model\",\"operator\":\"contain\",\"value\":[\"3900\"],\"parameterType\":\"fixed\" }}";

        String ClassName = "Router";
        System.out.println(SearchJson);
        URL url;
        HttpURLConnection connection = null;

        try {

            JSONParser parser = new JSONParser();
            JSONObject json = (JSONObject) parser.parse(SearchJson);

            String targetURL = "http://localhost:8080/cmdbuild/services/rest/v2/classes/"+ClassName+"/cards?CMDBuild-Authorization="
                    + SessionId;
            String urlParameters = json.toJSONString();

            url = new URL(targetURL);
            System.out.println(url);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type", "application/json");

            connection.setRequestProperty("Content-Length",
                    "" + Integer.toString(urlParameters.getBytes().length));
            connection.setRequestProperty("Content-Language", "en-US");

            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            // Send request
            DataOutputStream wr = new DataOutputStream(
                    connection.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();
            // Get Response

            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            // System.out.println("9"+rd.readLine());

            if (rd != null) {
                System.out.println(rd.readLine());
            }
            rd.close();

        } catch (Exception e) {
            System.out.println(e);
        }

    }

 

When I call this function, a new blank record has been added in to the Router class.

 

 

 

Hello Herru Windiarto,

 

Thanks for the reply...

 

I have got my session and as a parameter I am passing it to CMDB..

 

When I call the web services by passing filter as JSON, a new record got generated; while I am passing the same in the URL itself along with the session, error 505 returned.

 

Please help me by providing sample codes, if you have any..?

 

Thanks,

Sreekumar MP.

Hello,
 
I'm not sure what you mean by passing filter as JSON. Did you mean you query class using filter?
 
Before go to codes, have you tried using SoapUI? It will help you testing. I attach one sample REST request from SoapUI.
 
Regards,
Herru
 
Previously Sreekumar wrote:

Hello Herru Windiarto,

 

Thanks for the reply...

 

I have got my session and as a parameter I am passing it to CMDB..

 

When I call the web services by passing filter as JSON, a new record got generated; while I am passing the same in the URL itself along with the session, error 505 returned.

 

Please help me by providing sample codes, if you have any..?

 

Thanks,

Sreekumar MP.

 


cmdbuild_rest_soapui-png (27.2 KB)
Dear Sreekumar,
 
here is what we did:
 
$ curl "http://demo.cmdbuild.org/cmdbuild/services/rest/v2/sessions/" -H "Content-Type: application/json" --data-binary '{"username": "...", "password": "..."}' -X POST | json_pp 
{
   "data" : {
      ...
      "_id" : "s9c0qvjnk073tpj7d4pmig32hu"
   }
}
$ export REST_SESSION_ID=s9c0qvjnk073tpj7d4pmig32hu
$ curl "http://demo.cmdbuild.org/cmdbuild/services/rest/v2/classes/Employee/cards" -H "Content-Type: application/json" -H "CMDBuild-Authorization: $REST_SESSION_ID" -X GET | json_pp
{
   "meta" : {
      ...
 
      "total" : 10
   "data" : [
      ...
 
   ]
}
$ curl "http://demo.cmdbuild.org/cmdbuild/services/rest/v2/classes/Employee/cards" -H "Content-Type: application/json" -H "CMDBuild-Authorization: $REST_SESSION_ID" -X GET | json_pp 
{
   "meta" : {
      ...
 
      "total" : 10
   "data" : [
      ...
 
   ]
 
}
 
So, no new card is created after a GET request. Are you sure that you are doing a GET request and not a POST?
 
Best regards.
 
-- CMDBuild Team
 
Previously Sreekumar wrote:

Dear Team,

I was trying for search using filter [code has been attached here]

public void CmdbuildSerachMethod1(String SessionId) {
        String SearchJson = "{\"filter\": {\"attribute\":\"Model\",\"operator\":\"contain\",\"value\":[\"3900\"],\"parameterType\":\"fixed\" }}";

        String ClassName = "Router";
        System.out.println(SearchJson);
        URL url;
        HttpURLConnection connection = null;

        try {

            JSONParser parser = new JSONParser();
            JSONObject json = (JSONObject) parser.parse(SearchJson);

            String targetURL = "http://localhost:8080/cmdbuild/services/rest/v2/classes/"+ClassName+"/cards?CMDBuild-Authorization="
                    + SessionId;
            String urlParameters = json.toJSONString();

            url = new URL(targetURL);
            System.out.println(url);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type", "application/json");

            connection.setRequestProperty("Content-Length",
                    "" + Integer.toString(urlParameters.getBytes().length));
            connection.setRequestProperty("Content-Language", "en-US");

            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            // Send request
            DataOutputStream wr = new DataOutputStream(
                    connection.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();
            // Get Response

            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            // System.out.println("9"+rd.readLine());

            if (rd != null) {
                System.out.println(rd.readLine());
            }
            rd.close();

        } catch (Exception e) {
            System.out.println(e);
        }

    }

 

When I call this function, a new blank record has been added in to the Router class.