0

While posting a HTTPs request through apex I am getting below intermittent error .

*64545242 client sent invalid header line: "SFDC_STACK_DEPTH: 1" while reading client request headers, client: 154.53.405.31, server: localhost, request: "PUT /ad/tcse/ifinform/deal_info/1.0 HTTP/1.1"

I am calling the webservice class through javascript button and storing the result in variable as below

{!REQUIRESCRIPT("/soap/ajax/46.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/46.0/apex.js")}
var result =sforce.apex.execute("ApexClassName","FunctionName", {Parameter1:"{! Account.Id}",BooleanValue:true});

The request successfully gets posted but intermittently experiencing this response error which is not a 200 ok HTTPS response . Please let me know what can be the issue . I have no idea about how SFDC sets this header SFDC_STACK_DEPTH: 1"

global class ApexClassName
{
    
   
    //Caling this from JS button click 
    webservice static string FunctionName(string AccId,boolean BooleanValue)
    
    {
    
    HttpRequest req = new HttpRequest();
  req.setEndpoint(URL_String);  
  req.setMethod('PUT');  
  req.setHeader('Content-Type', 'application/json');
  req.setHeader('Accept', 'application/json;');

  req.setClientCertificateName('Certificate');    
  String requestBody  = Requeststring ;
  req.setTimeout(60000); 
  req.setBody(requestBody);  
  Http http = new Http();
  HttpResponse res = new HttpResponse();
  res = http.send(req);
  
  }
  }

Most of the requests are successful only few are giving a not 200 response . And the target system has the logs as "64545242 client sent invalid header line: "SFDC_STACK_DEPTH: 1" while reading client request headers, client: 154.53.405.31, server: localhost, request: "PUT /ad/tcse/ifinform/deal_info/1.0 HTTP/1.1"

2
  • This is to prevent the chaining of calls from one Salesforce server to another. Can you provide more context on the calling code for the HttpRequest so the community can better understand why the platform may be restricting you?
    – TSmith
    Oct 20 '20 at 10:58
  • @TSmith thanks a lot for the quick reply Smith . I have updated the query
    – user75937
    Oct 20 '20 at 11:41
0

I recommend adding the below check

RestRequest request = RestContext.request;
Map requestHeaders = request.headers;
if (requestHeaders.containsKey('Sfdc-Stack-Depth') && '1'.equals(requestHeaders.get('Sfdc-Stack-Depth'))){
System.debug('Do not calllout as it will cause a callout loop error');
}

As described here, The SFDC_STACK_DEPTH header is automatically added by any request that is made from a Salesforce server. Here is an example

POST /service/mywebservice HTTP/1.1
SFDC_STACK_DEPTH: 1
SOAPAction: ""
User-Agent: SFDC-Callout/29.0
Accept: text/xml
Content-Type: text/xml; charset=UTF-8
Cache-Control: no-cache
Pragma: no-cache
Host: example.com
Connection: keep-alive

It is used to prevent chaining of requests from one Salesforce server to another. Without such a mechanism threads could get blocked as they wait for other threads to complete. You could do things like chaining one anonymous Apex execution to another to work around other limits. So while you can make one level of calls from Salesforce to Salesforce, any further attempts will get blocked by the presence of the header.

See also:

Callout loop not allowed error

2
  • Thanks a lot Anudeep . Can you please help in what are the other values for SFDC_STACK_DEPTH other than 1 that SFDC sets
    – user75937
    Oct 23 '20 at 14:48
  • SFDC_STACK_DEPTH=1 is the header and there aren't any other values as far as I know Oct 26 '20 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.