0

I am trying to connect to an API of another company. from the doc there is ::

even with your GET request, you'll need to include the Java equivalent of curl_setopt($ch, CURLOPT_POSTFIELDS, $content), and you can set $data equal to an empty array.

$content in their example is an empty JSON array.

I am using org.apache.commons.httpclient.

i am not sure how to add post fields to a org.apache.commons.httpclient.methods.GetMethod or if it is even possible.

i tried faking with a Content-Length of 2 but the GET times out (probably looking for content that i am not providing. if i remove the content-length i get an invalid response from the api server)

   HttpClient client = new HttpClient();
   GetMethod method = new GetMethod("https://api.xxx.com/account/");
   method.addRequestHeader("Content-Type", "application/json");
   method.addRequestHeader("X-Public-Key", APKey);
   method.addRequestHeader("X-Signed-Request-Hash", "xxx");
   method.addRequestHeader("Content-Length", "2");

   int statusCode = client.executeMethod(method);
user207421
  • 289,834
  • 37
  • 266
  • 440
randy
  • 1,591
  • 2
  • 29
  • 66
  • If an empty JSON array is expected, wouldn't `[]` do the trick? – Robert Jan 10 '16 at 00:14
  • hey robert. thanks for responding. so this is the stupid question. How do I add an empty JSON array payload to a GetMethod? – randy Jan 10 '16 at 00:29
  • Do you need to pass that empty array as a URL query string? Does the API documentation say the name of that variable somewhere? Then you'd append `&variable=%5B%5D` to the URL. (%5B and %5D are the percent-encoded `[]`). – Robert Jan 10 '16 at 00:49

2 Answers2

0

I don't think GetMethod includes any means of attaching a request body, because a GET request isn't supposed to have a body. (But having a body isn't actually prohibited, either - see: HTTP GET with request body .)

You're trying to use documentation written with a different language and a different client library in mind, so you'll have to use trial and error a bit. It sounds like they expect a request with no body, and you already have that. There's no good reason why they'd require a "Content-Length" with GET, but if that's the case, try setting it to 0.

Community
  • 1
  • 1
Mike Baranczak
  • 7,894
  • 6
  • 39
  • 65
0

This is how i resolved this issue

Created this class

public class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {
    public HttpGetWithEntity() {
        super();
    }
    public HttpGetWithEntity(URI uri) {
        super();
        setURI(uri);
    }
    public HttpGetWithEntity(String uri) {
        super();
        setURI(URI.create(uri));
    }
    @Override
    public String getMethod() {
        return HttpGet.METHOD_NAME;
    }
}

Then the calling function looks like

public JSONObject get(JSONObject payload, String URL) throws Exception {

    JSONArray jsonArray = new JSONArray();
    CloseableHttpClient client = HttpClientBuilder.create().build();

    HttpGetWithEntity myGet = new HttpGetWithEntity(WeeblyAPIHost+URL);
    myGet.setEntity( new StringEntity("[]") );
    myGet.setHeader("Content-Type", "application/json");
    myGet.setHeader("X-Public-Key", APIKey);
    HttpResponse response = client.execute(myGet);

    JSONParser parser = new JSONParser();
    Object obj = parser.parse( EntityUtils.toString(response.getEntity(), "UTF-8") ) ;
    JSONObject jsonResponse = (JSONObject) obj;

    return jsonResponse;

}
randy
  • 1,591
  • 2
  • 29
  • 66