1

I have a REST Api which needs Accountid, oauth_consumer_key, oauth_consumer_secret and a url to retrieve the account details.

My apex code look like this

<apex:page>
    <b>Hello World!</b>
    <apex:form >
        <apex:commandButton value="Do something" action="{!doSomething}"/>
    </apex:form>
</apex:page>

and the Controller below

public with sharing class DetailButtonController
{

 String oauthkey='asdasdasd asd77cf6ef73b77';
 String oauthsecret='asdasdasdasd2195385b9aa2';
 String action='get';

        Http m_http = new Http();
        HttpRequest req = new HttpRequest();

        String content = 'action='+EncodingUtil.urlEncode(action, 'UTF-8')+'&oauth_consumer_key='+EncodingUtil.urlEncode(oauthkey, 'UTF-8')+'&oauth_consumer_secret='+EncodingUtil.urlEncode('oauthsecret', 'UTF-8');

        req.setEndpoint('http://www.abc.com/api/1.0/accounts/2323.xml/');
        req.setHeader('Content-Type','application/x-www-form-urlencoded');        
        req.setMethod('GET');
        req.setBody(content);

        httpResponse response = m_http.send(req);

}

So when the button is clicked it will call the url and read all xml responses.

There are 2 problems i am stuck on

  1. the controller gives Error: Compile Error: expecting a right parentheses, found 'http://www.abc.com/api/1.0/accounts/2323.xml/' at line 13 column 24

2.how to call this controller on click of that button

  • Can you post the Apex code you tried before? It'll make it easier to help you. – Sebastian Kessel Dec 28 '15 at 20:14
  • @SebastianKessel I have added the code and the 2 Questions – user580950 Dec 28 '15 at 20:21
2

That won't really compile... you don't have a method inside your class! :)

Try this instead (I am using the same "doSomething" method you already had):

public with sharing class DetailButtonController
{
  public string responseText {get;set;}

  public DetailButtonController() {
     responseText = '';
  } 

   public void doSomething() {

    String oauthkey='asdasdasd asd77cf6ef73b77';
    String oauthsecret='asdasdasdasd2195385b9aa2';
    String action='get';

    Http m_http = new Http();
    HttpRequest req = new HttpRequest();

    String content = 'action='+EncodingUtil.urlEncode(action, 'UTF-8')+'&oauth_consumer_key='+EncodingUtil.urlEncode(oauthkey, 'UTF-8')+'&oauth_consumer_secret='+EncodingUtil.urlEncode('oauthsecret', 'UTF-8');

    req.setEndpoint('http://www.abc.com/api/1.0/accounts/2323.xml/');
    req.setHeader('Content-Type','application/x-www-form-urlencoded');        
    req.setMethod('POST');
    req.setBody(content);

    httpResponse response = m_http.send(req);

    responseText = response.getBody();
  }

}

Change your page to be:

<apex:page controller="DetailButtonController">
    <b>Hello World!</b>
    <apex:form >
        <apex:commandButton value="Do something" action="{!doSomething}"/>
        <apex:outputPanel layout="block">
          {!responseText}
        </apex:outputPanel>
    </apex:form>
</apex:page>
| improve this answer | |
  • req.setMethod('GET'); should really be a POST so that the key & secret aren't exposed in the URL as visible parameters. – Mark Pond Dec 28 '15 at 20:33
  • Good point Mark, I just changed the code. I was trying to get the OP unstuck using the obvious solutions and didn't even pay attention to the code itself. – Sebastian Kessel Dec 28 '15 at 20:34
  • @SebastianKessel The error is gone, how do i print the response? – user580950 Dec 28 '15 at 21:00
  • I just changed my code to print the textual response from the API. Keep imn mind that there is no error handling or functionality in this sample. – Sebastian Kessel Dec 28 '15 at 21:05
  • @SebastianKessel Error: Unknown property 'DetailButtonController.responseText' – user580950 Dec 28 '15 at 21:16

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.