Hi Gurus ,
Basically I'm trying to simulate the below code from jsp in Java code by using apache httpclient . The code from simply does a post and behind the scens slingPostServlet takes care of rest based on params passed . So i'm trying to follow the same principle and using apache httpPost.execute (java code below) , however it returns a 401 error code indicating authentication error . i'm posting this to my local host cq , do i need to pass any extra headers as params to post method ? Any pointers ?
Code from jsp :
<form method="POST" action="/content" enctype="multipart/form-data"><input type="hidden" name=":operation" value="import" /><input type="hidden" name=":contentType" value="json" /><input type="hidden" name=":nameHint" value="sample" /><input type="text" name=":content" value="{ 'jcr:primaryType': 'nt:unstructured', 'propOne' : 'propOneValue', 'childOne' : { 'childPropOne' : true } }" /><input type="Submit" /></form>
Code in java :
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://localhost:4502/etc/commerce");
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair(":operation", "import"));
urlParameters.add(new BasicNameValuePair(":contentType", "json"));
urlParameters.add(new BasicNameValuePair(":nameHint", "test"));
urlParameters.add(new BasicNameValuePair(":content",{ 'jcr:primaryType': 'nt:unstructured', 'name' : 'pproduct1', 'childOne' : { 'childPropOne' : true } }));
httpPost.setEntity(new UrlEncodedFormEntity(urlParameters));
HttpResponse resp = httpClient.execute(httpPost);
Thanks in advance
Solved! Go to Solution.
The author instance requires login credentials so you have to submit them in some form.
If you eg. look at how the comments for blogposts work, you enter them in a "publish" environment (or rather via the dispatcher) where no login is required
and they are then reverse replicated back to author.
/Johan
Views
Replies
Total Likes
The author instance requires login credentials so you have to submit them in some form.
If you eg. look at how the comments for blogposts work, you enter them in a "publish" environment (or rather via the dispatcher) where no login is required
and they are then reverse replicated back to author.
/Johan
Views
Replies
Total Likes
Hi,
When you make httpclient call to CQ servlet or any CQ path in java file it requires cookie object. Because in CQ current session info stored in Cookie.
In java file you will not get the cookie object, so you are getting 401 unauthorized error.
If you do the same in Sling GET servlet, you can get the cookie from request object and set the cookie attribute in httpclient.
Hope it helps.
Thanks.
Vikram
Views
Replies
Total Likes
Please don't do this. I discussed the various reasons not to do it plus a "workaround" in [0]. And are you sure, that there is no service available which provides exactly the feature through java API, which you are trying to access via HTTP?
Jörg
[0] http://cqdump.wordpress.com/2012/08/01/cq5-requesting-itself/
Pls check you have correct ACL set at /etc/commerce or not?
Views
Replies
Total Likes
Make sure to set credentials in httpclient.
Views
Replies
Total Likes
Views
Likes
Replies