+ Start a Discussion
Ganesh Babu 7Ganesh Babu 7 

Apex rest callout - Sample

Can anyone help me on getting  below data , some sample endpoint URL and response as XML.
New to Integration, learning how the parse  json and xml from response. I have one url for json( https://api.androidhive.info/contacts/) but not for xml.

Thanks in Advance.....

HttpRequest req=new HttpRequest();
req.setendpoint(' ? ');
req.setmethod('GET');
http req1=new http();
httpResponse xmlresponse=req1.send(req);

 
Best Answer chosen by Ganesh Babu 7
mukesh guptamukesh gupta
Hi Ganesh,

Please use below code for xml parsing:-
 
Map < String, String > mapCustomer = new Map < String, String >();
String endpoint = 'http://www.thomas-bayer.com/sqlrest/CUSTOMER/1/';
HTTP h = new HTTP();
HTTPRequest req = new HTTPRequest();
req.setEndPoint(endpoint);
req.setMethod('GET');
HTTPResponse res = h.send(req);
Dom.Document doc = res.getBodyDocument();
Dom.XMLNode customer = doc.getRootElement();
for(Dom.XMLNode child : customer.getChildElements()) {
       mapCustomer.put(child.getName(), child.getText());
}
system.debug(mapCustomer);

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 

All Answers

Steven NsubugaSteven Nsubuga
public class ApexCalloutsDemo {

    public static HttpResponse makeCallout() {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://api.androidhive.info/contacts/');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        
        // If the request is successful, parse the JSON response.
        if (response.getStatusCode() == 200) {
            // Deserializes the JSON string into collections of primitive data types.
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            // Cast the values in the 'contacts' key as a list
            List<Object> contacts = (List<Object>) results.get('contacts');
            System.debug('Received the following contacts:');
            for (Object contact: contacts) {
                System.debug(contact);
            }
        }
        return response;
    }
      
}

 
mukesh guptamukesh gupta
Hi Ganesh,

Please use below code for xml parsing:-
 
Map < String, String > mapCustomer = new Map < String, String >();
String endpoint = 'http://www.thomas-bayer.com/sqlrest/CUSTOMER/1/';
HTTP h = new HTTP();
HTTPRequest req = new HTTPRequest();
req.setEndPoint(endpoint);
req.setMethod('GET');
HTTPResponse res = h.send(req);
Dom.Document doc = res.getBodyDocument();
Dom.XMLNode customer = doc.getRootElement();
for(Dom.XMLNode child : customer.getChildElements()) {
       mapCustomer.put(child.getName(), child.getText());
}
system.debug(mapCustomer);

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 
This was selected as the best answer
Ganesh Babu 7Ganesh Babu 7
Thanks Steven .....Actually i need some Sample endpoint URL's  that return XML response.( no authentication , no setbody).
I have searched it in google, but i am not getting one to test it.

Thanks again....
Ganesh Babu 7Ganesh Babu 7
Thank lot.. Steven and Mukesh.