Reputation: 11855
I am trying to post XML data as a body to a REST api.
I have a method that creates the 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 the 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 is the method.setRequestEntity is saying that the method could not be found.
I have
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.*;
If i set method = PostMethod instead of method = doREST it works but I don't want to have to do that in all my other methods just to create queries.
Is there something I am missing as to why the method.setRequestEntity is not working the way it is right now?
EDIT: I got my information for using setRequestEntity from PostMethod setRequestBody(String) deprecated - why?
EDIT 2: Here is what I ended up 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;
}
Upvotes: 2
Views: 11621
Reputation: 28638
You should modify doREST
to accept the RequestEntity
instead of a boolean
. Pass in null
for a GET and a value for POST. Use that as the check to see if you need a PostMethod
or a GetMethod
. Then you can have the specific type so you can call the PostMethod
only setRequestEntity()
.
EDIT:
You can avoid the cast like 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;
}
Upvotes: 3
Reputation: 66089
The method you should be calling is setEntity
, not setRequestEntity
. Also, your body should be wrapped in a StringEntity
, not a StringRequestEntity
, imported from org.apache.http.entity
.
Upvotes: 1