function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
motti10motti10 

How can I call an External REST API Webservice to return data so I can use it in Salesforce

Thanks..

 

I may be missing something, but i am trying to post to this example URL


'http://]URL]/services/v1/rest/Scripto/execute/HelloWorld?username=[user]&password=V[pwd]&foo=hello')

 

It will return "hello"

 

Here is my "Groovy code that works)

 

 import org.apache.commons.httpclient.Credentials
 import org.apache.commons.httpclient.HostConfiguration
 import org.apache.commons.httpclient.HttpClient
 import org.apache.commons.httpclient.UsernamePasswordCredentials
 import org.apache.commons.httpclient.auth.AuthScope
 import org.apache.commons.httpclient.methods.GetMethod
 import org.apache.commons.httpclient.methods.PostMethod
 import org.apache.commons.httpclient.NameValuePair
 
 
 public static String httpGet(String urlStr) throws IOException {
  URL url = new URL(urlStr);
  HttpURLConnection conn =
      (HttpURLConnection) url.openConnection();

  if (conn.getResponseCode() != 200) {
    throw new IOException(conn.getResponseMessage());
  }

  // Buffer the result into a string
  BufferedReader rd = new BufferedReader(
      new InputStreamReader(conn.getInputStream()));
  StringBuilder sb = new StringBuilder();
  String line;
  while ((line = rd.readLine()) != null) {
    sb.append(line);
  }
  rd.close();

  conn.disconnect();
  return sb.toString();
}

httpGet ('http://]URL]/services/v1/rest/Scripto/execute/HelloWorld?username=[user]&password=V[pwd]&foo=hello')

 

Can I implement this same thing?



Pat PattersonPat Patterson

Hi motti10,

 

The HTTP callout sample in the docs does pretty much exactly what your Groovy does - http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http.htm

 

Cheers,

 

Pat