TalibGill TalibGill - 4 months ago 34
reST (reStructuredText) Question

java.lang.IllegalArgumentException: Invalid uri '/Assignment/rest/words/{name}/protection': escaped absolute path not valid

i have spring restful web service, which is working fine with the postman(plugin in chrome) with URL (http://localhost:8080/Assignment/rest/words/{name}/protection) which requires 1 parameter named as "name"

and i have httpclient written in java too. with the following code.

HttpClient client = new HttpClient();

PostMethod method = new PostMethod("http://localhost:8080/Assignment/rest/words/{name}/protection");
method.setParameter("name", "value");

byte[] responseBody = null;
int statusCode = 0;
try {
// Executing method.
statusCode = client.executeMethod(method);
}catch(Exception e){}
// checking status code
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}

// Reading response body.
responseBody = method.getResponseBody();


on
statusCode = client.executeMethod(method);
its shows error as follows

java.lang.IllegalArgumentException: Invalid uri 'http://localhost:8080/Assignment/rest/words/{name}/protection': escaped absolute path not valid org.apache.commons.httpclient.HttpMethodBase.<init>(HttpMethodBase.java:222) org.apache.commons.httpclient.methods.ExpectContinueMethod.<init>(ExpectContinueMethod.java:93) org.apache.commons.httpclient.methods.EntityEnclosingMethod.<init>(EntityEnclosingMethod.java:119) org.apache.commons.httpclient.methods.PostMethod.<init>(PostMethod.java:106) com.aspose.talib.service.WordService.getDataFromURL(WordService.java:50) com.aspose.talib.service.WordService.postRequestForProtectData(WordService.java:37) com.aspose.talib.web.WordController.init(WordController.java:21) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:606) javax.el.BeanELResolver.invoke(BeanELResolver.java:183) javax.el.CompositeELResolver.invoke(CompositeELResolver.java:161) org.apache.el.parser.AstValue.getValue(AstValue.java:173) org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184) com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109) javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194) javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182) javax.faces.component.UIOutput.getValue(UIOutput.java:174) com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205) com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355) com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164) javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:920) javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863) javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859) javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859) com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:458) com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:134) com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120) com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219) javax.faces.webapp.FacesServlet.service(FacesServlet.java:659) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


Please help

Answer

Obviously, characters like curly braces is not allowed in the URI. You have to encode that part.

Try something like this:

PostMethod method = new PostMethod(URIUtil.encodePath("http://localhost:8080/Assignment/rest/words/{name}/protection", "UTF-8"));
method.setParameter("name", "value");

URIUtil is from commons-httpclient-3.1.

    <dependency>
        <groupId>commons-httpclient</groupId>
        <artifactId>commons-httpclient</artifactId>
        <version>3.1</version>
    </dependency>