i trying post xml data body rest api.
i have method creates request called dorest.
string url = null; httpmethod method; log.info("query: " + query); if (stringutil.isempty(query)) { url = buildrequesturl("/issues.ashx/issues/mywork"); method = dorest(url, false); } else { url = buildrequesturl("/issues.ashx/issues/filters"); //method = dorest(url, true); method = dorest(url, true); string xml = "<issuesfilteren>" + "<issueid>" + query + "</issueid>" + "</issuesfilteren>"; requestentity entity = new stringrequestentity(xml,"text/xml; charset=iso-8859-1", null); method.setrequestentity(entity); } and dorest method
private httpmethod dorest(string request, boolean post) throws exception { string uri = request; httpmethod method = post ? new postmethod(uri) : new getmethod(uri); configurehttpmethod(method); httpclient client = gethttpclient(); client.getparams().setparameter(httpclientparams.so_timeout, timeoutlength); client.executemethod(method); return method; } my issue method.setrequestentity saying method not found.
i have
import org.apache.commons.httpclient.httpclient; import org.apache.commons.httpclient.httpmethod; import org.apache.commons.httpclient.methods.*; if set method = postmethod instead of method = dorest works don't want have in other methods create queries.
is there missing why method.setrequestentity not working way right now?
edit: got information using setrequestentity postmethod setrequestbody(string) deprecated - why?
edit 2: here ended doing.
private httpmethod dorest(string request, requestentity entity) throws exception { string uri = request; httpmethod method; if ( entity != null ){ method = new postmethod(uri); ((postmethod) method).setrequestentity(entity); } else { method = new getmethod(uri); } configurehttpmethod(method); httpclient client = gethttpclient(); client.getparams().setparameter(httpclientparams.so_timeout, timeoutlength); client.executemethod(method); return method; }
you should modify dorest accept requestentity instead of boolean. pass in null , value post. use check see if need postmethod or getmethod. can have specific type can call postmethod setrequestentity().
edit:
you can avoid cast this:
private httpmethod dorest(string request, requestentity entity) throws exception { string uri = request; httpmethod method; if ( entity != null ){ postmethod postmethod = new postmethod(uri); postmethod.setrequestentity(entity); method = postmethod; } else { method = new getmethod(uri); } configurehttpmethod(method); httpclient client = gethttpclient(); client.getparams().setparameter(httpclientparams.so_timeout, timeoutlength); client.executemethod(method); return method; }
Comments
Post a Comment