cancel
Showing results for 
Search instead for 
Did you mean: 

Pass a JSON object to the script

anusk
Champ in-the-making
Champ in-the-making
I want to call a webscript from java, but there are some that I don't know how to do it.
For example, I call the webscript to delete a user with no problem, and it works fine, but reading the code of the "PUT /alfresco/service/api/people/{userName}" webscript, the parameter in the URL is only the name of the user and it uses a json object to change the properties…

if (!json.isNull("lastName"))
   {
      person.properties["lastName"] = json.get("lastName");
   }

so if I want to call that script to change the lastname…How would I call the webscript?

Thanks in advance,
Ana
4 REPLIES 4

rwetherall
Confirmed Champ
Confirmed Champ
Hi,

You call a Webscript in the same way you call any URL from Java.

The JSON you need to send to the server makes up the content of the request.

This http://wiki.alfresco.com/wiki/3.0_REST_API#Person_Service might help.

Cheers,
Roy

anusk
Champ in-the-making
Champ in-the-making
I don't know what I'm doing wrong, but while DELETE and GET method works, PUT method doesn't.
With this code

HttpClient client = new HttpClient();
Credentials defaultcreds = new UsernamePasswordCredentials("admin", "admin");
client.getState().setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), defaultcreds);
GetMethod get = new GetMethod("http://localhost:8080/alfresco/service/api/people/Jhon");
       get.setDoAuthentication(true);
      
       try {
                                                  // execute the GET
          int status = client.executeMethod(get);
                            
           // print the status and response
           System.out.println(status + "\n" +
                 get.getResponseBodyAsString());
          
       } catch (HttpException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } finally {
           // release any connection resources used by the method
         get.releaseConnection();
       }

The method get works, and retrieve the data of Jhon, but when I call the PUT:


HttpClient client = new HttpClient();
Credentials defaultcreds = new UsernamePasswordCredentials("admin", "admin");
client.getState().setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), defaultcreds);
PutMethod put = new PutMethod("http://localhost:8080/alfresco/service/api/people/Jhon");
   //   put.addRequestHeader(new Header("lastName", "newLastName"));
       put.setDoAuthentication(true);
   
      
       try {
                                                 // execute the PUT
          int status = client.executeMethod(put);
                            
           // print the status and response
           System.out.println(status + "\n" +
                 put.getResponseBodyAsString());
          
       } catch (HttpException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } finally {
           // release any connection resources used by the method
         put.releaseConnection();
       }


I get this response…

200
{
   "url": "\/alfresco\/service\/api\/person\/admin",
   "userName": "admin",
   "enabled": true,
   "firstName": "Administrator",
   "lastName": "",
   "jobtitle": null,
   "organization": null,
   "location": null,
   "telephone": null,
   "mobile": null,
   "email": "admin@alfresco.com",
   "companyaddress1": null,
   "companyaddress2": null,
   "companyaddress3": null,
   "companypostcode": null,
   "companytelephone": null,
   "companyfax": null,
   "companyemail": null,
   "skype": null,
   "instantmsg": null,
   "quota": -1,
   "sizeCurrent": 12760934,
   "persondescription": null
}

I tried to put the data in the header, but nothing happens…and why it goes to \/alfresco\/service\/api\/person\/admin instead of Jhon???

Kind Regards,
Ana

dc_noze
Champ in-the-making
Champ in-the-making
Hi Ana,
I don't know exactly what is going on, but about the request you are missing some steps:
- you have to set request CONTENT-TYPE to application/json
- set the request BODY with the json representing the person data you want to update

This is a snippet with a sample request taken from http://svn.alfresco.com/repos/alfresco-open-mirror/alfresco/HEAD/root/projects/remote-api/source/jav...

private JSONObject updatePerson(String userName, String title, String firstName, String lastName,
            String organisation, String jobTitle, String email, String bio, String avatarUrl, int expectedStatus)
    throws Exception
    {
        // switch to admin user to create a person
        String currentUser = this.authenticationComponent.getCurrentUserName();
        String adminUser = this.authenticationComponent.getSystemUserName();
        this.authenticationComponent.setCurrentUser(adminUser);
       
        JSONObject person = new JSONObject();
        person.put("userName", userName);
        person.put("title", title);
        person.put("firstName", firstName);
        person.put("lastName", lastName);
        person.put("organisation", organisation);
        person.put("jobtitle", jobTitle);
        person.put("email", email);
       
        Response response = sendRequest(new PutRequest(URL_PEOPLE + "/" + userName, person.toString(), "application/json"), expectedStatus);
       
        // switch back to non-admin user
        this.authenticationComponent.setCurrentUser(currentUser);
       
        return new JSONObject(response.getContentAsString());
    }

I hope this can help.

anusk
Champ in-the-making
Champ in-the-making
Hi dc_noze,
Sorry for the late post, but I've been quite busy.
I set the request entity like this:


JSONObject person = new JSONObject();
             person.put("lastName","NEW_LAST_NAME");
             put.setRequestEntity(new StringRequestEntity(person.toString(),"application/json", null));
then execute the method and finally it works!
Thank you very much for your response.

Regards,
Ana