cancel
Showing results for 
Search instead for 
Did you mean: 

Looking for sample JSON request to upload documents.

srinivasr
Champ on-the-rise
Champ on-the-rise
Hi,

I am looking for a sample json request to upload the documents into alfresco.


Could someone help me creating a JSON request to upload documents using SOAP UI or any REST Client.




I could upload the documents using html form.

Request:
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
    <title>File Upload</title>
   </head>
<body>
    <form method="POST" name="uploadForm" enctype='multipart/form-data' id="upload_form" action="https://alfresco-stage:8443/alfresco/service/api/upload">
         <input type="hidden" name="destination" id="destination" value="workspace://SpacesStore/cea187e1-b73d-43c7-a2d6-1ec7b1c37715"/>
        <label>
            Name:
        </label>
        <br/>
        <input type="text" name="filename" id="filename"/><p/>
        <label>
            Description:
        </label>
        <br/>
        <textarea name="description" id="description"></textarea>
        <p/>
        <label>
            File:
        </label>
        <br/>
        <input class="button" type="file" name="filedata" id="filedata"/><p/>
      <input class="button" type="submit" name="submit" value="Upload"/>
    </form>
</body>
</html>


Response:
{
   "nodeRef": "workspace://SpacesStore/f24e5dc3-f8c6-427b-8cee-bec0236543a2",
   "fileName": "SrinivasDocument",
   "status":
   {
      "code": 200,
      "name": "OK",
      "description": "File uploaded successfully"
   }
}

1 REPLY 1

kaynezhang
World-Class Innovator
World-Class Innovator
Try

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.methods.GetMethod;
import org.json.JSONException;
import org.json.JSONObject;

      client.getState().setCredentials(
            new AuthScope("localhost", 8080, "Alfresco"),
            new UsernamePasswordCredentials("admin", "admin"));

      String apiurl =  "http://localhost:8080/alfresco/service/api/upload";

         PostMethod mPost = new PostMethod(urlString);
         // File f1 =fileobj;
         Part[] parts = {
               new FilePart("filedata", filename, fileobj, filetype, null),
               new StringPart("filename", filename),
               new StringPart("description", description),
               
               //new StringPart("destination", "workspace://SpacesStore/ce55a850-b1c7-43c7-a600-b5c58a985429"),
               new StringPart("uploaddirectory", "TESTFOLDER1/"),

               // modify this according to where you wanna put your content
               new StringPart("siteid", "kaynezhang"),
               new StringPart("containerid", "documentLibrary"),
         };
         mPost.setRequestEntity(new MultipartRequestEntity(parts, mPost
               .getParams()));
         System.out.println(mPost.getRequestEntity());
         int statusCode1 = client.executeMethod(mPost);

         mPost.releaseConnection();