cancel
Showing results for 
Search instead for 
Did you mean: 

Problem getting parameters in web script

spilby
Confirmed Champ
Confirmed Champ
Hi,

I don't know why I can't obtain a parameter in my java backed web script.

I do this to call my java backed webscript by REST:

   HttpClient client = new HttpClient(); 
   PostMethod method = new PostMethod("http://IP/alfresco/service/mywebscript");

   // 2 ways to set de parameter. No one works!
   client.getParams().setParameter("name", "George");
   method.setParameter("name", "George");

   int statusCode = client.executeMethod(method);  


And on the java webscript side, I would to obtain the parameter doing this:

   public void execute(WebScriptRequest req, WebScriptResponse res) throws WebScriptException
   {
    (…)
    String param =  req.getParameter("name")
    (…)
   }

But the param is null! What I'm doing wrong??

If I call the url throw the Firefox, doing "http://IP/alfresco/service/mywebscript?name=George" the param isn't null and arrives George. And if I do

   PostMethod method = new PostMethod("http://IP/alfresco/service/mywebscript?name=George");

it works fine too. Why don't works using the setParameter?? I don't understand.

Thanks a lot!
4 REPLIES 4

kaynezhang
World-Class Innovator
World-Class Innovator
You can try with

HttpClient client = new HttpClient();
PostMethod method = new PostMethod("http://IP/alfresco/service/mywebscript");
method.setQueryString(new NameValuePair[] {
    new NameValuePair("name", "George")
});            

Yes! This solution resolves the problem. With setQueryString the parameter arrives to the Webscript. Thank you very much!

But why client.getParams().setParameter or method.setParameter don't works? You know the reason?

I try this:

   JSONObject data = new JSONObject();
   data.put("test", "this is a test");
   method.setRequestEntity(new StringRequestEntity(data.toString(), "application/json", "UTF-8"));

but don't works. Only the setQueryString works.


kaynezhang
World-Class Innovator
World-Class Innovator
Following code will add ("test", "this is a test") to http request body.

JSONObject data = new JSONObject();
data.put("test", "this is a test");
method.setRequestEntity(new StringRequestEntity(data.toString(), "application/json", "UTF-8"));


setParameter is used to set http request header,for example

setParameter("Connection", "Keep-Alive");

spilby
Confirmed Champ
Confirmed Champ
Oks, I understand. Thanks for your help! Best regards! Smiley Happy