+ Start a Discussion
wixxeywixxey 

No response after making a POST request

Hi!

I am calling an API of FluidSurvey. when i make a POST request ... it post the request on the fluidSurvey but i didnt 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(20000);
            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="newFS">
<center>
      <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>
      </center>
</apex:page>

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

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

You're setting a content-length header, but not setting any body,the server is dilligently waiting for the 3199 byte body you said you were going to send to turn up, and so you get the timeout exception you're seeing.

 

Also res.toString() will just return the server stautus like (200 OK, etc), not the actual response body, you need to use getBody() for that.

All Answers

gbu.varungbu.varun

Hi Wixxey,

 

In debug log check are able to hit the URL and try to debug for response

System.debug(res.getBody());

 

Regards,

Varun

wixxeywixxey

This is the Log

 

27.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
12:40:48.015 (15725000)|EXECUTION_STARTED
12:40:48.015 (15766000)|CODE_UNIT_STARTED|[EXTERNAL]|06690000002aEZs|VF: /apex/NewFS
12:40:48.016 (16040000)|VF_DESERIALIZE_VIEWSTATE_BEGIN|06690000002aEZs
12:40:48.018 (18934000)|VF_DESERIALIZE_VIEWSTATE_END
12:40:48.020 (20402000)|CODE_UNIT_STARTED|[EXTERNAL]|01p90000002nhD6|newFS invoke(chk)
12:40:48.020 (20733000)|METHOD_ENTRY|[1]|01p90000002nhD6|newFS.newFS()
12:40:48.020 (20744000)|METHOD_EXIT|[1]|newFS
12:40:48.020 (20794000)|METHOD_ENTRY|[9]|01p90000002nhD6|newFS.getData()
12:40:48.020 (20904000)|SYSTEM_METHOD_ENTRY|[22]|System.HttpRequest.setEndpoint(String)
12:40:48.020 (20960000)|SYSTEM_METHOD_EXIT|[22]|System.HttpRequest.setEndpoint(String)
12:40:48.020 (20997000)|SYSTEM_METHOD_ENTRY|[23]|System.HttpRequest.setTimeout(Integer)
12:40:48.021 (21033000)|SYSTEM_METHOD_EXIT|[23]|System.HttpRequest.setTimeout(Integer)
12:40:48.021 (21073000)|SYSTEM_METHOD_ENTRY|[24]|System.HttpRequest.setMethod(String)
12:40:48.021 (21108000)|SYSTEM_METHOD_EXIT|[24]|System.HttpRequest.setMethod(String)
12:40:48.021 (21391000)|SYSTEM_METHOD_ENTRY|[26]|system.EncodingUtil.base64Encode(Blob)
12:40:48.021 (21473000)|SYSTEM_METHOD_EXIT|[26]|system.EncodingUtil.base64Encode(Blob)
12:40:48.021 (21522000)|SYSTEM_METHOD_ENTRY|[27]|System.HttpRequest.setHeader(String, String)
12:40:48.021 (21561000)|SYSTEM_METHOD_EXIT|[27]|System.HttpRequest.setHeader(String, String)
12:40:48.021 (21595000)|SYSTEM_METHOD_ENTRY|[28]|System.HttpRequest.setHeader(String, String)
12:40:48.021 (21629000)|SYSTEM_METHOD_EXIT|[28]|System.HttpRequest.setHeader(String, String)
12:40:48.021 (21663000)|SYSTEM_METHOD_ENTRY|[29]|System.HttpRequest.setHeader(String, String)
12:40:48.021 (21693000)|SYSTEM_METHOD_EXIT|[29]|System.HttpRequest.setHeader(String, String)
12:40:48.021 (21721000)|SYSTEM_METHOD_ENTRY|[31]|System.Http.send(ANY)
12:40:48.021 (21825000)|CALLOUT_REQUEST|[31]|System.HttpRequest[Endpoint=https://ezqcp.fluidsurveys.com/api/v2/surveys/26814/responses/, Method=POST]
12:41:08.806 (20806009000)|EXCEPTION_THROWN|[31]|System.CalloutException: Read timed out
12:41:08.806 (20806138000)|SYSTEM_METHOD_EXIT|[31]|System.Http.send(ANY)
12:41:08.806 (20806224000)|SYSTEM_METHOD_ENTRY|[47]|String.valueOf(Object)
12:41:08.806 (20806263000)|SYSTEM_METHOD_EXIT|[47]|String.valueOf(Object)
12:41:08.806 (20806282000)|SYSTEM_METHOD_ENTRY|[47]|System.debug(ANY)
12:41:08.806 (20806295000)|USER_DEBUG|[47]|DEBUG|Callout error: System.CalloutException: Read timed out
12:41:08.806 (20806301000)|SYSTEM_METHOD_EXIT|[47]|System.debug(ANY)
12:41:08.806 (20806323000)|METHOD_ENTRY|[48]|01p90000002nhD6|newFS.__sfdc_tst()
12:41:08.806 (20806374000)|METHOD_EXIT|[48]|01p90000002nhD6|newFS.__sfdc_tst()
12:41:08.806 (20806395000)|SYSTEM_METHOD_ENTRY|[48]|String.valueOf(Object)
12:41:08.806 (20806402000)|SYSTEM_METHOD_EXIT|[48]|String.valueOf(Object)
12:41:08.806 (20806411000)|SYSTEM_METHOD_ENTRY|[48]|System.debug(ANY)
12:41:08.806 (20806417000)|USER_DEBUG|[48]|DEBUG|null--------null
12:41:08.806 (20806421000)|SYSTEM_METHOD_EXIT|[48]|System.debug(ANY)
12:41:08.806 (20806431000)|METHOD_EXIT|[9]|01p90000002nhD6|newFS.getData()
12:41:08.810 (20810324000)|CODE_UNIT_FINISHED|newFS invoke(chk)
12:41:08.811 (20811919000)|VF_APEX_CALL|j_id7|{!chk}|PageReference: none
12:41:08.833 (20833810000)|CODE_UNIT_STARTED|[EXTERNAL]|01p90000002nhD6|newFS get(tst)
12:41:08.833 (20833834000)|SYSTEM_MODE_ENTER|true
12:41:08.833 (20833848000)|CODE_UNIT_STARTED|[EXTERNAL]|01p90000002nhD6|tst
12:41:08.833 (20833865000)|CODE_UNIT_FINISHED|tst
12:41:08.833 (20833873000)|CODE_UNIT_FINISHED|newFS get(tst)
12:41:08.835 (20835562000)|VF_SERIALIZE_VIEWSTATE_BEGIN|06690000002aEZs
12:41:08.847 (20847048000)|VF_SERIALIZE_VIEWSTATE_END
12:41:08.849 (20849093000)|CODE_UNIT_STARTED|[EXTERNAL]|Generate Metadata
12:41:08.850 (20850658000)|CODE_UNIT_FINISHED|Generate Metadata
12:41:09.056 (20853889000)|CUMULATIVE_LIMIT_USAGE
12:41:09.056|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of code statements: 19 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 1 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

12:41:09.056|CUMULATIVE_LIMIT_USAGE_END

12:41:08.853 (20853936000)|CODE_UNIT_FINISHED|VF: /apex/NewFS
12:41:08.853 (20853952000)|EXECUTION_FINISHED
gbu.varungbu.varun

Hi,

 

put reg.setTimeOut(2000); in your code. I think headervalues are not seted properly so you are getting read time out error.

 

 

 

 

 

wixxeywixxey

i have set the timeout to 20000 but it still has the same problem

gbu.varungbu.varun

Hi wixxey,

 


You are not passing api key,survey id etc.  required variable to pass in URL. I just find api documentation on Goolg. You should pass  required variables in URL.

Here is link for api:

http://fluidsurveys.com/api/

wixxeywixxey

 This version of the API is no longer supported. they are using a new version now. if you click on the link you have given contain the path of their new api documentation

 

SurpriseSurprise

Hey,

 

Is your  request from salesforce trying to access something which is running behind firewall?

 

 

 

SuperfellSuperfell

You're setting a content-length header, but not setting any body,the server is dilligently waiting for the 3199 byte body you said you were going to send to turn up, and so you get the timeout exception you're seeing.

 

Also res.toString() will just return the server stautus like (200 OK, etc), not the actual response body, you need to use getBody() for that.

This was selected as the best answer
gbu.varungbu.varun

 

Hi,

 

Remove ezqcp. from URL and try the following url

 

https://fluidsurveys.com/api/v2/surveys/26814/responses/

 

 

wixxeywixxey

After setting the URL the new Log is

 

27.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
19:15:17.023 (23233000)|EXECUTION_STARTED
19:15:17.023 (23271000)|CODE_UNIT_STARTED|[EXTERNAL]|06690000002aEZs|VF: /apex/newFS
19:15:17.024 (24363000)|VF_DESERIALIZE_VIEWSTATE_BEGIN|06690000002aEZs
19:15:17.032 (32650000)|VF_DESERIALIZE_VIEWSTATE_END
19:15:17.034 (34959000)|PUSH_TRACE_FLAGS|[EXTERNAL]|01p90000002nhD6|newFS|APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
19:15:17.034 (34986000)|CODE_UNIT_STARTED|[EXTERNAL]|01p90000002nhD6|newFS invoke(chk)
19:15:17.035 (35478000)|METHOD_ENTRY|[1]|01p90000002nhD6|newFS.newFS()
19:15:17.035 (35495000)|METHOD_EXIT|[1]|newFS
19:15:17.035 (35563000)|PUSH_TRACE_FLAGS|[8]|01p90000002nhD6|newFS|APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
19:15:17.035 (35580000)|METHOD_ENTRY|[8]|01p90000002nhD6|newFS.getData()
19:15:17.035 (35762000)|SYSTEM_METHOD_ENTRY|[22]|System.HttpRequest.setEndpoint(String)
19:15:17.035 (35805000)|SYSTEM_METHOD_EXIT|[22]|System.HttpRequest.setEndpoint(String)
19:15:17.035 (35843000)|SYSTEM_METHOD_ENTRY|[24]|System.HttpRequest.setMethod(String)
19:15:17.035 (35877000)|SYSTEM_METHOD_EXIT|[24]|System.HttpRequest.setMethod(String)
19:15:17.036 (36205000)|SYSTEM_METHOD_ENTRY|[26]|system.EncodingUtil.base64Encode(Blob)
19:15:17.036 (36281000)|SYSTEM_METHOD_EXIT|[26]|system.EncodingUtil.base64Encode(Blob)
19:15:17.036 (36325000)|SYSTEM_METHOD_ENTRY|[27]|System.HttpRequest.setHeader(String, String)
19:15:17.036 (36354000)|SYSTEM_METHOD_EXIT|[27]|System.HttpRequest.setHeader(String, String)
19:15:17.036 (36382000)|SYSTEM_METHOD_ENTRY|[28]|System.HttpRequest.setHeader(String, String)
19:15:17.036 (36402000)|SYSTEM_METHOD_EXIT|[28]|System.HttpRequest.setHeader(String, String)
19:15:17.036 (36431000)|SYSTEM_METHOD_ENTRY|[29]|System.HttpRequest.setHeader(String, String)
19:15:17.036 (36456000)|SYSTEM_METHOD_EXIT|[29]|System.HttpRequest.setHeader(String, String)
19:15:17.036 (36481000)|SYSTEM_METHOD_ENTRY|[30]|System.HttpRequest.setHeader(String, String)
19:15:17.036 (36500000)|SYSTEM_METHOD_EXIT|[30]|System.HttpRequest.setHeader(String, String)
19:15:17.036 (36527000)|SYSTEM_METHOD_ENTRY|[31]|System.HttpRequest.setCompressed(Boolean)
19:15:17.036 (36547000)|SYSTEM_METHOD_EXIT|[31]|System.HttpRequest.setCompressed(Boolean)
19:15:17.036 (36565000)|SYSTEM_METHOD_ENTRY|[33]|System.Http.send(ANY)
19:15:17.036 (36643000)|CALLOUT_REQUEST|[33]|System.HttpRequest[Endpoint=https://app.fluidsurveys.com/api/v2/surveys/184404/responses/, Method=POST]
19:15:27.622 (10622853000)|EXCEPTION_THROWN|[33]|System.CalloutException: Read timed out
19:15:27.623 (10623033000)|SYSTEM_METHOD_EXIT|[33]|System.Http.send(ANY)
19:15:27.623 (10623140000)|SYSTEM_METHOD_ENTRY|[55]|String.valueOf(Object)
19:15:27.623 (10623183000)|SYSTEM_METHOD_EXIT|[55]|String.valueOf(Object)
19:15:27.623 (10623206000)|SYSTEM_METHOD_ENTRY|[55]|System.debug(ANY)
19:15:27.623 (10623214000)|USER_DEBUG|[55]|DEBUG|Callout error: System.CalloutException: Read timed out
19:15:27.623 (10623221000)|SYSTEM_METHOD_EXIT|[55]|System.debug(ANY)
19:15:27.623 (10623233000)|METHOD_EXIT|[8]|01p90000002nhD6|newFS.getData()
19:15:27.623 (10623248000)|POP_TRACE_FLAGS|[8]|01p90000002nhD6|newFS|APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
19:15:27.625 (10625358000)|CODE_UNIT_FINISHED|newFS invoke(chk)
19:15:27.625 (10625382000)|POP_TRACE_FLAGS|[EXTERNAL]|01p90000002nhD6|newFS|
19:15:27.626 (10626359000)|VF_APEX_CALL|j_id8|{!chk}|PageReference: none
19:15:27.647 (10647983000)|CODE_UNIT_STARTED|[EXTERNAL]|01p90000002nhD6|newFS get(tst)
19:15:27.648 (10648008000)|SYSTEM_MODE_ENTER|true
19:15:27.648 (10648022000)|CODE_UNIT_STARTED|[EXTERNAL]|01p90000002nhD6|tst
19:15:27.648 (10648039000)|CODE_UNIT_FINISHED|tst
19:15:27.648 (10648049000)|CODE_UNIT_FINISHED|newFS get(tst)
19:15:27.648 (10648134000)|CODE_UNIT_STARTED|[EXTERNAL]|01p90000002nhD6|newFS get(result)
19:15:27.648 (10648146000)|SYSTEM_MODE_ENTER|true
19:15:27.648 (10648157000)|CODE_UNIT_STARTED|[EXTERNAL]|01p90000002nhD6|result
19:15:27.648 (10648166000)|CODE_UNIT_FINISHED|result
19:15:27.648 (10648173000)|CODE_UNIT_FINISHED|newFS get(result)
19:15:27.650 (10650047000)|VF_SERIALIZE_VIEWSTATE_BEGIN|06690000002aEZs
19:15:27.661 (10661977000)|VF_SERIALIZE_VIEWSTATE_END
19:15:27.663 (10663873000)|CODE_UNIT_STARTED|[EXTERNAL]|Generate Metadata
19:15:27.665 (10665304000)|CODE_UNIT_FINISHED|Generate Metadata
19:15:28.029 (10668499000)|CUMULATIVE_LIMIT_USAGE
19:15:28.029|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of code statements: 19 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 1 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

19:15:28.029|CUMULATIVE_LIMIT_USAGE_END

19:15:27.668 (10668529000)|CODE_UNIT_FINISHED|VF: /apex/newFS
19:15:27.668 (10668538000)|EXECUTION_FINISHED
RocketRocket

Have u incorportaed changes suggested by Simon?

wixxeywixxey

Thanx SimonF ... your suggestion works for me after setting the getBody method my code works.. and thanx to all others who give there precious time