Apache httpclient post throws resposne status 401 Unauthorized error | Community
Skip to main content
Level 4
October 16, 2015
Solved

Apache httpclient post throws resposne status 401 Unauthorized error

  • October 16, 2015
  • 5 replies
  • 5846 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Ojjis

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

5 replies

Ojjis
OjjisAccepted solution
Level 7
October 16, 2015

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

vikramca06
Level 4
October 16, 2015

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

joerghoh
Adobe Employee
Adobe Employee
October 16, 2015

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/

Level 6
October 16, 2015

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

Sham_HC
Level 10
October 16, 2015

Make sure to set credentials in httpclient.

http://hc.apache.org/httpclient-3.x/authentication.html