cancel
Showing results for 
Search instead for 
Did you mean: 

how to test POST webscript?

targa2000
Champ in-the-making
Champ in-the-making
I can test a GET webscript with : http://localhost:8080/alfresco/service/somegetwebscript

how can test a POST webscript?

I tried

http://localhost:8080/alfresco/service/somepostwebscript

but get

Web Script Status 405 - Method Not Allowed

The Web Script /alfresco/service/somepostwebscript has responded with a status of 405 - Method Not Allowed.

405 Description:   The method specified in the Request-Line is not allowed for the resource identified by the Request-URI.

Message:   02130004 Script url /somepostwebscript does not support the method GET
   
Exception:   org.alfresco.web.scripts.WebScriptException - 02130004 Script url /somepostwebscript does not support the method GET
   
   org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntime.java:152)
   org.alfresco.web.scripts.servlet.WebScriptServlet.service(WebScriptServlet.java:122)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
   org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
   org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
   org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
   java.lang.Thread.run(Thread.java:619)
7 REPLIES 7

zladuric
Champ on-the-rise
Champ on-the-rise
Your problem is that you're using GET request for POST webscript. Use POST http request.

Make yourself a bit of javascript somewhere where you can call this easily for testing:

var url = "http://localhost:8080/alfresco/service/somepostwebscript";
var post_field = "foo=1&bar=2";
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", post_field.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
   if(http.readyState == 4 && http.status == 200) {
      alert(http.responseText);
   }
}
http.send(params);

mikeh
Star Contributor
Star Contributor
You can also use "alf_method=post" on the URL, which web scripts support in order to allow PUT and DELETE methods from clients that don't natively support them (e.g. Adobe AIR)

Thanks,
Mike

targa2000
Champ in-the-making
Champ in-the-making
Your problem is that you're using GET request for POST webscript. Use POST http request.

Make yourself a bit of javascript somewhere where you can call this easily for testing:

var url = "http://localhost:8080/alfresco/service/somepostwebscript";
var post_field = "foo=1&bar=2";
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", post_field.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
   if(http.readyState == 4 && http.status == 200) {
      alert(http.responseText);
   }
}
http.send(params);

Can I use the above call to a webscript in the alfresco repository from a dashlet?

zladuric
Champ on-the-rise
Champ on-the-rise
Can I use the above call to a webscript in the alfresco repository from a dashlet?

Yes, to do that, you should put that as a part of a dashlet javascript (client side .js I think - the one you include in dashlet.get.head.ftl or just include it as inline javascript in dashlet.het.html.ftl).

gyro_gearless
Champ in-the-making
Champ in-the-making
For issuing POST requests, you may take CURL as commandline tool, e.g.

curl -u admin:admin -F hopp=topp -F foo=bar -F upload=@some.doc http://localhost:8080/alfresco/service/de/acme-corp/simple-upload

will do a simple POST form request with 2 parameters and one file upload.

See http://curl.haxx.se/ for more on curl 🙂

HTH
Gyro

targa2000
Champ in-the-making
Champ in-the-making
Can I use the above call to a webscript in the alfresco repository from a dashlet?

Yes, to do that, you should put that as a part of a dashlet javascript (client side .js I think - the one you include in dashlet.get.head.ftl or just include it as inline javascript in dashlet.het.html.ftl).

I tried it but was not successful.  Maybe you would know what I have done wrong?

following is the webscript I deployed on alfresco:

test-post-ws.post.desc.xml

<webscript>
  <shortname>Test Post Webscript</shortname>
  <description>Test post webscript</description>
  <url>/test-post/test-post-ws</url>
  <authentication>user</authentication>
  <transaction>required</transaction>
</webscript>

test-post-ws.post.desc.xml

<#escape x as jsonUtils.encodeJSONString(x)>

   "success": ${success?string},
   "message": "<#if errormsg??>${errormsg}</#if>"
}
</#escape>

test-post-ws.post.js

var logFile = "test_Log.txt";

var folderPath = "Guest Home";
var folderNode = companyhome.childByNamePath(folderPath);

var outputFile = folderNode.childByNamePath(logFile);

if (outputFile == null) {
  outputFile = folderNode.createFile(logFile);
}

if (folderNode === null) {
   folderNode = companyhome.createFolder(folderPath);
}

try {
       outputFile.content +="\n";
   var currentdate = new Date().toGMTString();
   outputFile.content += currentdate;   
   outputFile.content +="\n";   
       model.success = true;
} catch (e) {
   outputFile.content +="exception in test : ";   
   outputFile.content += new Date().toGMTString();   
   outputFile.content +="\n";
   outputFile.content +="exception occurred: ";   
   outputFile.content += e.message;   
   outputFile.content +="\n";       
   
    // error
    model.success = false;
    model.errormsg = "Error - " + e.message;   
}   

the webscript deployed without problems.  following is the dashlet I deployed to test it:

test-post-dashlet.get.desc.xml

<webscript>
   <shortname>Test Post WS Dashlet</shortname>
   <description>Test post ws dashlet</description>
   <family>user-dashlet</family>
   <url>/test-post/test-post-dashlet</url>
</webscript>

test-post-dashlet.get.html.ftl

<div class="dashlet">
   <div class="title">Test Post WS Dashlet</div>
<script type="text/javascript">   
   function testClicked(form) {
     var url = "http://localhost:8080/alfresco/service/test-post/test-post-ws";
          alert(url);
          var post_field = "foo=1&bar=2";
          alert(post_field);
     http.open("POST", url, true);
     alert("after http.open");
    
     //Send the proper header information along with the request
     http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
     http.setRequestHeader("Content-length", post_field.length);
     http.setRequestHeader("Connection", "close");

     http.onreadystatechange = function() {//Call a function when the state changes.
            if(http.readyState == 4 && http.status == 200) {
            alert(http.responseText);
            }
         }
      http.send(params);   
   }   
</script>
<form id="test-post" action="" method="post">
<p>                                                                
                <label>Name</label>
                <input type="text" name="name" id="name" /> </p>                                                                                   
<p>                          
                <input type="submit" name="submit" id="new-submit" value="save" />
                <input type="button" name="test-post-submit" id="test-post-submit" value="test" onClick="testClicked(this.form)" />                
                <input type="button" name="cancel" id="new-cancel" value="cancel" />
             </p>
         </form>
</div>

the dashlet deploys and appears on the dashboard without problems.  nothing happens after http.open.  It doesn't seem to make the connection.  Nothing happens on the alfresco webscript side as well.  Should I try it in a different way?

kpatel
Confirmed Champ
Confirmed Champ

You can Take url form all web scripts.

And u se the postman which is google crome extension.