Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

I am calling an API of FluidSurvey. When I make a POST request ... it post the request on the fluidSurvey but I didn't get the JSON response. rather it returns nothing. any suggestion??

my controller code

public class fluidSurvey{

public String tst{set;get;}

public String result{get;set;}

public PageReference chk() {

    getData();

    return null;

}

public void getData(){

    String apiKey = 'xxxxxx';

    String pwd = 'xxxxxx';

    String u = 'https://app.fluidsurveys.com/api/v2/surveys/survey_id/responses/';

    HttpRequest req = new HttpRequest();

    Http http = new Http();

    HTTPResponse res;

    try{

        req.setEndPoint(u);

        req.setTimeout(2000);

        req.setMethod('POST');

        Blob headerValue = Blob.valueOf(apikey + ':' + pwd);

        String authorizationHeader = 'Basic '+ EncodingUtil.base64Encode(headerValue);

        req.setHeader('Authorization', authorizationHeader);

        req.setHeader('Content-Type', 'application/json');

        req.setHeader('Content-Length','31999');

        res = http.send(req); 

        tst= res.toString();

       catch(Exception e){

           System.debug('Callout error: '+ e);

           System.debug(tst+'--------'+res);

       }     

    }

}

and the Apex page code is

<apex:page controller="fluidSurvey">

<apex:form >

    <apex:pageBlock title="New Fluid Surveys API">

    <apex:outputText value="{!tst}"></apex:outputText><br/>

<apex:pageBlockButtons location="bottom">

    <apex:commandButton value="Submit" action="{!chk}"/>

</apex:pageBlockButtons>   

</apex:pageBlock>

</apex:form>

and API documentation link is http://docs.fluidsurveys.com/api/surveys.html#getting-a-list-of-surveys..

1 Answer

0 votes
by (32.1k points)
edited by

The problem with my code was that I setting a content-length header, but not setting anybody, the server is diligently waiting for the 3199-byte body. so after using the set body method my code properly returns a JSON response

Looking for a comprehensive Salesforce course? Enroll now!

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...