1
votes

Can anyone share an end to end example for making a JSON webservice Call Through Apex ( Visual Force Pages and Controllers ) in Salesforce . Pretty Much like we do in HTML5 ,Jquery by Ajax !

1
For RestFul Webservices and SOAP Webservices. - Learner

1 Answers

2
votes

There are examples right in the documentation of calling REST web services.

From HTTP Classes:

public class HttpCalloutSample {

// Pass in the endpoint to be used using the string url
  public String getContent(String url) {

// Instantiate a new http object
    Http h = new Http();

// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
    HttpRequest req = new HttpRequest();
    req.setEndpoint(url);
    req.setMethod('GET');

// Send the request, and return a response
    HttpResponse res = h.send(req);
    return res.getBody();
  }
}

You can change the method to one of:

GET, POST, PUT, DELETE, TRACE, CONNECT, HEAD, and OPTIONS

A more complete example is available at HTTP (RESTful) Services

There is also support for JSON deserialization.

Don't forget to use the Remote Site Settings to open up access to the target domain.

For a SOAP web service you can define Apex classes from a WSDL.