You need to sign in to do that
Don't have an account?
Turner Nelson
How can I wait for an Http Response inside a future?
I currently have a problem involving triggers, futures, callouts, and HttpResponses.
Currently, when my trigger is called, it calls a future function, which contains a callout to make a POST Http Request. This POST request is expecting a Http Response. From what I've been researching, my method is completing before the response comes back, which should only take several seconds, but the future is complete before it responds. I've seen there are continuation object options as well, but since futures are Static methods, I'm not sure that is an option. Here is what I have so far:
I've tried to make a wait() function, but this function doesn't work when the 'HttpResponse response = http.send(request);' line is not commented out.
Any thoughts or suggestions how to overcome this? I'm open to using Continuations, but unsure of when they are to be used.
Currently, when my trigger is called, it calls a future function, which contains a callout to make a POST Http Request. This POST request is expecting a Http Response. From what I've been researching, my method is completing before the response comes back, which should only take several seconds, but the future is complete before it responds. I've seen there are continuation object options as well, but since futures are Static methods, I'm not sure that is an option. Here is what I have so far:
Public class Provision_Trial_Trigger_API_Callout { @future(callout=true) public static Void execute(Map<String, String> config) { try { Http http = new Http(); HttpRequest request = new HttpRequest(); request.setEndpoint(config.get('endpoint')); request.setMethod(config.get('method')); request.setBody(config.get('body')); config.remove('endpoint'); config.remove('method'); config.remove('body'); for (String key : config.keySet()) { request.setHeader(key, config.get(key)); } HttpResponse response = http.send(request); //the above line appears to be causing the function to stop //and no line below it or above it will execute if not commented out if (response.getStatusCode() == 200){ //do stuff }else{ System.debug('fail'); } } catch(DmlException e) { System.debug('The following exception has occurred: ' + e.getMessage()); } } }
I've tried to make a wait() function, but this function doesn't work when the 'HttpResponse response = http.send(request);' line is not commented out.
Any thoughts or suggestions how to overcome this? I'm open to using Continuations, but unsure of when they are to be used.
from there you can use ..
If I put this following line inside the if state to print the response body, then I check the Developer Console, it never reaches that line, or the 'fail' comment. It appears the HttpResponse response = http.send(request); line is halting the execution.
Refer this code
// as default may not be long enough,
HttpRequest request = new HttpRequest();
request.setEndpoint(config.get('endpoint'));
request.setMethod(config.get('method'));
request.setBody(config.get('body'));
request.setTimeout(60000); // sets timeout to 60 seconds <-- ADD THIS LINE
// THEN: Add a while loop immediately after sending the http request and wrap the request in a try catch block like so,
// declare an HttpResponse object without instantiating it
// so you can access it outside of the try/catch if necessary
HttpResponse response;
try{
// instantiate HttpResponse object
response = http.send(request);
// add the following while loop after sending request
while(response.getBody() == null) {
// Wait for the response to complete by waiting for the reponse body to be set.
// No need to do anything else in this wait loop.
}
system.debug('request completed successfully!');
}
catch{
// if 60 seconds elapses without response
// the request will timeout
// handle request timeout failure here
system.debug('request failed!');
}
// nice :-)