function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NHKNHK 

Continuation Error: StatusCode=400

Hi All,

I am using Continuation for a callout in a VF page. I am getting a blank response body with StatusCode=400 . When i call the same service using normal HttpRequest, i am getting the proper response. Did any one came across a similar situation? Please help me resolve this error.
public object startLandAServiceRequest(){
	Continuation con = new Continuation(60);
	try{
		con.continuationMethod = 'processResponse';
		HttpRequest request = new HttpRequest();
		request.setMethod('GET');
		request.setEndpoint('xxxx');
		request.setClientCertificateName('xxxx');
		request.setHeader('Content-type','text/xml');
		request.setBody('xxxx');
		this.requestLabel = con.addHttpRequest(request);
        }catch(Exception e){
			isError = true;
			errorString = e.getMessage();
            System.debug(e.getStackTraceString());
        }
	return con;
}
public object processResponse(){
	HttpResponse response = Continuation.getResponse(this.requestLabel);
        result = response.getBody();
        return null;
}



Best,
NHK
Abhishek_DEOAbhishek_DEO
Have you defined the variable "requestLabel" in upper class, if so I would suggest you to add debug statement to see the value of this variable "requestLabel" in method "processResponse"
NHKNHK
Yes i have checked that and it is printing "Continuation-1" in both the methods.
MohandaasMohandaas
NHK,

Status code 400 implies that the external system could not understand your request. It considers as bad.
Generally GET method does not require a body as part of the request. I would advice you to try without body and content-type header.
Also, please paste the code you use to call the service using http GET method.

Good luck.
NHKNHK
Hi Mohan,
Please find below the code for Normal Http Request.
public void startLandAServiceRequest(){
        try{
            String envelope = buildHeader();
            String reqBody = buildbody();
            envelope += reqBody;
            HttpRequest httpRequest = getHttpRequest();
            httpRequest.setBody(envelope);
            HttpResponse response = h.send(httpRequest);
            processResponse(response);
        }catch(Exception e){
            isError = true;
            errorString = e.getMessage();
            System.debug(e.getStackTraceString());
        }
}
private HttpRequest getHttpRequest(){
        HttpRequest httpRequest = new HttpRequest();
        httpRequest.setMethod('GET');
        httpRequest.setEndpoint('xxxx');
        httpRequest.setClientCertificateName('xxxx');
        httpRequest.setHeader('Content-type','text/xml');
        return httpRequest;
}
In buildHeader and buildBody methods i am actually building the request body in order to pass the security token and other information which is mandatory for the user identification by the endpoint. 
 
NHKNHK
I have raised a case with Salesforce and below is the response that i got from SF.

The limitation with Apex Continuations is regarding Named Credentials. 

Presently, Named Credentials work only with regular Apex callouts. To use a client certificate in an Apex continuation, a regular Remote Site Setting URL needs to be used, and the client certificate can be set in the HttpRequest's properties either by developer name or by a base64 string of a PKCS#12 file plus that PKCS#12 file's password.

Named Credentials are somewhat new, though they have been around for several releases. Named Credentials offer a convenient way to specify the client certificate and any HTTP-level authentication within a Named Credential configuration, and Apex code can make use of that configuration by referencing the named credential in the callout URL. That feature, however, doesn't yet work with Apex Continuation callouts.
NHKNHK
The issue was with the improper request body and hence i was getting error. When the request body is formatted properly, i am able to get the proper response.
Abhishek_DEOAbhishek_DEO
Are you saying that it worked with named credential and issue was with htttp body only?
NHKNHK
No Named credentials are not supported in Continuation. The issue was with improperly framed body. One of the tag in the XML body was improper and i didn't noticed it when i compare.