Apache httpclient post throws resposne status 401 Unauthorized error
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