cancel
Showing results for 
Search instead for 
Did you mean: 

Posting form data to a web script

stiegld
Champ in-the-making
Champ in-the-making
Hi folks,

Happy holidays to all.

I am trying to write a webscript that accepts formdata from a form. The webapp sending the form, however, uses a commons-httpclient proxy to do authentication and to separate the alfresco instance from the web application.

I can't seem to get the web script to accept any arguments. Here is an excerpt from the webscript code:


script:
{

var authorEmail = null;
var content = null;
var comment = null;

for (arg in args)
{
  logger.log(arg + "=" + args[arg]);
}

// log each argument (assuming one or more values have been provided for each)
for (arg in argsM)
{
  for each (val in argsM[arg])
  {
     logger.log(arg + "=" + val);
  }
}

// locate file attributes
for each (field in formdata.fields)
{
  if (logger.isLoggingEnabled()) {
    logger.log("found field "+field.name+" with value "+field.value);
  }
  if (field.name == "email")
  {
    authorEmail = field.value;
  }
  else if (field.name == "comment")
  {
    content = field.value;
  }
}

if (logger.isLoggingEnabled()) {
//    logger.log("formdata is +"formdata.fields);
   logger.log("email is "+authorEmail);
   logger.log("content is "+content);
}

As you can see, this excerpt simply tries to display parameters submitted, and the only output is

email is null
content is null

The commons-http client excerpt is below:


   
      client.getHttpConnectionManager().getParams()
            .setConnectionTimeout(5000);

      client.getState().setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(username, password));
      PostMethod post = new PostMethod(url);
      post.setDoAuthentication(true);

      post.setRequestHeader("Content-Type", "multipart/form-data");

      Part[] parts = { new StringPart("email", authorEmail),
            new StringPart("comment", comment) };
      post.setRequestEntity(new MultipartRequestEntity(parts, post
            .getParams()));

      try {
         int status = client.executeMethod(post);

         System.out.println("received status " + status);

         if (status > 299) { // there was an error
               System.out.println(post.getResponseBodyAsString());
         }
      } finally {
         post.releaseConnection();
      }
   }

I've also tried to send data using NameValuePairs and the setRequestBody () method, to no avail. I know that the Content-Type is set correctly because if I change it or remove it, the formdata object is never initialized in the script.

Any ideas?

Dan
4 REPLIES 4

dooley
Champ in-the-making
Champ in-the-making
try this out


//create HttpClient object
HttpClient client = new HttpClient();

//pre-authenticate with alfresco server
client.getParams().setAuthenticationPreemptive(true);

Credentials loginCreds = new UsernamePasswordCredentials("admin", "admin");

//Use AuthScope.ANY since the HttpClient connects to just one URL
client.getState().setCredentials(AuthScope.ANY , loginCreds);

PostMethod post= new PostMethod(url);

post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                                new DefaultHttpMethodRetryHandler(3, false));

//Alfresco only reads the POST parameters if they are in the body of the request, so we need to create a RequestEntity to populate the body.

Part[] parts = {new StringPart("email", authorEmail), new  StringPart("submit", "Post"), new StringPart("comment", comment)};

        MultipartRequestEntity entity = new MultipartRequestEntity(parts, post.getParams());

post.setRequestEntity(entity);

try
        {
            //subimit the request and get the response code
            int statusCode = client.executeMethod(method);
            //Check for code 200
            if (statusCode != HttpStatus.SC_OK)
            {
                throw new HttpException("HTTP returned status code " + statusCode);
            }

            InputStream responseBody = post.getResponseBodyAsStream();
}

stiegld
Champ in-the-making
Champ in-the-making
I removed the Content-Type property altogether and also added the preemptive authentication flag and it started working.

Thanks for the reply!

Dan

P.S. I'm having a problem using the createAssociation method in the ScriptNode API in a web script (no association is created, no error is thrown). I wonder if anyone else has this problem or it's a known issue.

kevinr
Star Contributor
Star Contributor
I have run through the createAssociation test scripts and they execute ok. Which version of Alfresco are you using? Can you provide example of your code/model?

Thanks,

Kevin

stiegld
Champ in-the-making
Champ in-the-making
Hi Kevin,

Upon revisiting this, I've found it to work now with no changes (I guess it worked before and something else had been wrong).

Dan