[SalesForce] Rest API call from apex Controller

I want to call a url using Rest API from my apex controller and show the response from that url to my visualforce page. I have written a code. But it is not working.

Controller

public class RestApi_Controller{
    public HTTPResponse res{get; set;}
    public String resBody{get; set;}
    public String endpoint{get; set;}

        @future(callout=true)
        public static void submit(){
            HttpRequest req = new HttpRequest();
            req.setMethod('GET');                                    
            //Seting HttpRequest Method
            req.setHeader('content-type', 'application/json');       
            //Seting HttpRequest header properties
            req.setHeader('Content-Length', '2000');
            req.setEndpoint('https://cosprdapi.cadence.com/cdpaccess/Ldap_FolderUpload/CreateSearchUserServlet?uid=******&pwd=*****' );
            Http http = new Http();
            try{
                 HTTPResponse res = http.send(req);                 
                 //Executing web service call
                 System.debug('STATUS:' + res.getStatus());
                 System.debug('STATUS_CODE:' + res.getStatusCode());
            }
            catch(System.CalloutException e){        {
                //Exception handling goes here..
                system.debug(e);
            }
     }
}

Here is my vf page:

enter image description here

Best Answer

You are sending some extra parameters I remove them and got 200 status.

HttpRequest req = new HttpRequest();
req.setMethod('GET');     
//you don't need this                               
//Seting HttpRequest Method
//req.setHeader('content-type', 'application/json');       
//Seting HttpRequest header properties
//req.setHeader('Content-Length', '2000');
req.setEndpoint('https://cosprdapi.cadence.com/cdpaccess/Ldap_FolderUpload/CreateSearchUserServlet?uid=*****&pwd=******' );
Http http = new Http();
try{
    HTTPResponse res = http.send(req);                 
    //Executing web service call
    System.debug('STATUS:' + res.getStatus());
    System.debug('STATUS_CODE:' + res.getStatusCode());
}
catch(System.CalloutException e){
    //Exception handling goes here..
    system.debug(e);
}

Here are the success status enter image description here

Note: Also NEVER share your account credentials in public site. I hope they are test credentials.

Related Topic