1

I have requirement that I need to fetch the data form salesforce DB. My input Ids would be more than 1000+. Hence I would like to pass this list of Ids in post method.

GET method is failing since it exceeds the limit.

Can someone help me on this?

David Sam
  • 49
  • 6

1 Answers1

2

I'm assuming from your question that some (but not all) GET requests to SalesForce are already working, so you already have most of the code needed to talk to SalesForce and you just need the gaps filling on how to make a POST request instead of a GET request.

I hope the following code provides some demonstration of this. Note that it is untested as I don't presently have access to a SalesForce instance to test it against:

import org.apache.http.HttpHeaders;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.message.BasicNameValuePair;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class HttpPostDemo {

    public static void main(String[] args) throws Exception {

        String url = ... // TODO provide this.

        HttpPost httpPost = new HttpPost(url);
        // Add the header Content-Type: application/x-www-form-urlencoded; charset=UTF-8.
        httpPost.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.withCharset(StandardCharsets.UTF_8).getMimeType());

        // Construct the POST data.
        List<NameValuePair> postData = new ArrayList<>();
        postData.add(new BasicNameValuePair("example_key", "example_value"));
        // add further keys and values, the one above is only an example.

        // Set the POST data in the HTTP request.
        httpPost.setEntity(new UrlEncodedFormEntity(postData, StandardCharsets.UTF_8));

        // TODO make the request...
    }
}

It is perhaps worth pointing out that in essence the code isn't much different to that in a related question which appears in the sidebar.

Community
  • 1
  • 1
Luke Woodward
  • 56,377
  • 16
  • 76
  • 100
  • Thanks for the answer. Here I am unable to use execute(). When I try to initialize as HttpClient httpClient = new DefaultHttpClient(); it is showing as depecrated. Can you please suggest me? @Luke Woodward – David Sam Jan 04 '16 at 04:55
  • 1
    @DavidSam: can you use `HttpClient httpclient = HttpClients.createDefault();` as in the answer I linked to? – Luke Woodward Jan 04 '16 at 21:56