user2584920 user2584920 - 3 months ago 18
HTTP Question

Send content body with HTTP GET Request in Java

I have a third party API which needs content body in a http GET request and doesn't accept any other forms of input.I cant make any changes to this API and the owners are not taking change requests to the API. I know that this is a bad design and all but is there any way this could be done ? I have tried apache httpurlconnection and java url apis, but haven't been able to achieve the same.

Answer

You need to use the below

public class HttpGetWithBody extends HttpEntityEnclosingRequestBase {

    @Override
    public String getMethod() {
        return "GET";
    }
}

HttpGetWithBody getWithBody = new HttpGetWithBody ();
getWithBody.setEntity(y(new ByteArrayEntity(
            "<SOMEPAYLOAD FOR A GET ???>".toString().getBytes("UTF8"))););
getResponse = httpclient.execute(getWithBody );

Import needed will be org.apache.http.client.methods.HttpEntityEnclosingRequestBase;