Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.CalloutException: For input string: "[email protected]"

I am trying to access the fluid survey API in Sales force. I use the following code but it responds with an error.

The code for controller class is:

public class fluidSurvey{
    public String tst{set;get;}
    public String result{get;set;}

    public PageReference chk() {

        //if (!ApexPages.hasMessages())
            result=getData();

        return null;
    }
 public String getData()
   {
       HttpRequest req= new HttpRequest();
       Http http = new Http();
       req.setMethod('GET');
       String url = 'https://username:[email protected]/api/v2/surveys/';
       req.setEndpoint(url);
       HttpResponse res = http.send(req);
       String json =  res.getBody().replace('\n','');
       tst = json;
        try {
            JSONObject j = new JSONObject( json );
            return parseJson(j);
        } catch (JSONObject.JSONException e) {
            return 'Error parsing JSON response from Google: '+e;
        }

   }

   public String parseJson(JSONObject resp){
       String detail =resp.getString('total');

       return detail;
   }
}

and the code for apex page is:

<apex:page controller="fluidSurvey">
  <!-- Begin Default Content REMOVE THIS -->
   <h1>Fluid Survey</h1>
   <apex:form >
       <apex:outputText value="{!tst}"></apex:outputText>
      <apex:commandButton value="Submit" action="{!chk}"/>
  </apex:form>
</apex:page>

but when I click the submit button it create the following error:

System.CalloutException: For input string: "[email protected]"
Error is in expression '{!chk}' in component <apex:page> in page fluid-page
like image 581
WiXXeY Avatar asked Aug 15 '26 20:08

WiXXeY


1 Answers

You need to explicitly set the Authorization header.

Try the following code:

public String getData()
{
    HttpRequest req= new HttpRequest();
    Http http = new Http();
    req.setMethod('GET');
    String authString = 'username:password';
    String url = 'https://app.fluidsurveys.com/api/v2/surveys/';
    req.setEndpoint(url);

    req.setHeader('Authorization', 'BASIC ' + EncodingUtil.base64Encode(Blob.valueOf(authString)));

    HttpResponse res = http.send(req);
    String json =  res.getBody().replace('\n','');

    try {
        JSONObject j = new JSONObject( json );
        return parseJson(j);
    } catch (JSONObject.JSONException e) {
        return 'Error parsing JSON response from Google: '+e;
    }
}
like image 124
aminland Avatar answered Aug 17 '26 09:08

aminland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!