2

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;
    }
2
  • What version of httpclient are you using? There are some API naming changes between 3.x (apache-commons httpclient) and 4.x (apache httpcomponents httpclient). Commented Apr 29, 2011 at 17:29
  • Apache commons httpclient 3.1 Commented Apr 29, 2011 at 17:56

2 Answers 2

3

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Don't think that will work because in doREST I would need to return a PostMethod instead of HttpMethod.
I went with this approach but was still having issues so I ended up casting the method to PostMethod if an entity was passed then setting the setRequestEntity.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.