0

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

1

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.

0

Your fundamental issue is that you are making the callout in an @future context... e.g. asynchronously. If you want the controller to return the results to the VFP, then the callout has to be made synchronously. You should be able to simply remove the @future(callout=true) annotation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.