cancel
Showing results for 
Search instead for 
Did you mean: 

Issue in creating blog using REST API

pradeepsimha
Champ in-the-making
Champ in-the-making
Hi,

I am trying to create blog post programmatically using REST API, the url is like below:

/alfresco/service/api/blog/site/{site}/{container}/posts


When I post the URL I am getting below exception:


{
    "status": {
        "code": 500,
        "name": "Internal Error",
        "description": "An error inside the HTTP server which prevented it from fulfilling the request."
    },
    "message": "04060020 Wrapped Exception (with status template): null",
    "exception": "org.springframework.extensions.webscripts.WebScriptException - 04060020 Wrapped Exception (with status template): null",
    "callstack": [
        "",
        "java.lang.NullPointerException",
        "org.alfresco.repo.web.scripts.blogs.posts.BlogPostsPost.parsePostParams(BlogPostsPost.java:95)",
        "org.alfresco.repo.web.scripts.blogs.posts.BlogPostsPost.executeImpl(BlogPostsPost.java:72)",
        "org.alfresco.repo.web.scripts.blogs.AbstractBlogWebScript.executeImpl(AbstractBlogWebScript.java:294)",
        "org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:64)",
        "org.alfresco.repo.web.scripts.RepositoryContainer$3.execute(RepositoryContainer.java:429)",
        "org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:450)",
        "org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:491)",
        "org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:529)",
        "org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:341)",
        "org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:378)",
        "org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:209)",
        "org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:132)",
        "javax.servlet.http.HttpServlet.service(HttpServlet.java:727)",


I tried to add parameters like title, content etc. But still I get the same error. My question is, what is the correct syntax for creating blog post using REST API? This URL: http://docs.alfresco.com/4.1/index.jsp?topic=%2Fcom.alfresco.enterprise.doc%2Freferences%2FRESTful-i... doesn't explain in brief what all the parameters I need to pass. Can anyone kindly guide me in this?
2 REPLIES 2

kaynezhang
World-Class Innovator
World-Class Innovator
Nearly all parameter is nullable ,but make sure set content type to application/json.

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

      String site = "kaynezhang";
      String container = "blog";
      String apiurl = "http://localhost:8080/alfresco/service/api/blog/site/'+ site + "/" + container + "/posts";
      PostMethod post = new PostMethod(apiurl);
      try {
         JSONObject blog = new JSONObject();
         blog.put("title", "title");
         blog.put("content", "content");
         JSONArray arr = new JSONArray();
         arr.put("tag1");
         arr.put("tag2");
         blog.put("tags", arr);
         blog.put("draft", false);

         post.setDoAuthentication(true);
         post.setRequestHeader("Content-Type", "application/json");// make sure set content type to application/json
         post.setRequestEntity(new StringRequestEntity(blog.toString(),"application/json", "UTF-8"));

         int status = client.executeMethod(post);
         if (status != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + post.getStatusLine());
         }
         String resultString = post.getResponseBodyAsString();
         System.out.println(resultString);
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         post.releaseConnection();
      }

Excellent !! Thanks for this post. I made a miss with Content-Type Smiley Happy