Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Apache httpclient post throws resposne status 401 Unauthorized error

Avatar

Level 4

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

1 Accepted Solution

Avatar

Correct answer by
Level 7

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

View solution in original post

5 Replies

Avatar

Correct answer by
Level 7

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

Avatar

Level 4

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

Avatar

Employee Advisor

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/

Avatar

Level 6

Pls check you have correct ACL set at /etc/commerce or not?