Freshdesk renderData function "Cannot set headers after they are sent to the client"

Hello. I am writing an app that needs to pull information from two external APIs. Rather than doing this on the front-end of the Freshdesk app, I am adding a serverless function since API keys are needed to access each. After receiving the response from the second API call, I get the following error when calling the renderData function to return the results back to the front-end:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:535:11)

If I call either API on their own withing the serverless function, it will work properly. However, when both are called this error will occur. If I try to have two separate serverless functions (one for each call), it will encounter the error as well. Here is a rough outline of what the code looks like:

var fetch = require('node-fetch');
exports = {
getData:  async (options) => {
      const base_url = 'url';
      const code = 'code';
      let url = base_url + code;
      let request_options = {
        headers:{
          'Key': 'key'
        }
      }
      var data_set_1;
      await fetch(url, request_options)
        .then(response => response.json())
        .then(data => {
          data_set_1 = data;
        });
      let id
      // set id based on first api call
      url = base_url + code + '/person/' + id
      var data_set_2;
      await fetch(url, request_options)
        .then(response => response.json())
        .then(data => {
          data_set_2 = data.body;
        });
      let results
      // assigning values to results
      renderData(null, JSON.stringify(results));
  }
}

Thanks.

@Dan_Herr

Welcome to the Freshworks developer forum :slight_smile:

Just a clarification, did you try using renderData(null, results); [assuming results is a JSON object] instead of a JSON string(renderData(null, JSON.stringify(results)))
if not could you please try that once! and let us know?

Stay safe :slight_smile:

@velmurugan

Thank you for your response. Sadly, I have tried having results as a JSON object and using renderData(null, results) and have gotten the same error.

In the code of the original post, results was a javascript object, so I had to use JSON.stringify().

The HTTP headers error occurs regardless of what I try to return (ex. even renderData(error) causes the error.)

Hi @Dan_Herr,
The SMI needs to be executed within 5 sec. If that function is not returning data within 5 sec, you will see this error.
Make sure that your backend function should complete its execution within 5 seconds or it will timeout.
Return the data in JSON format

4 Likes

Hi @Sheik,

Thank you for your response. That makes sense. It looks like the two combined calls take about 7 sec total to complete. Is there a way to increase this time, or is that not an option? Thanks!

1 Like

@Dan_Herr, in your case, Have a common backend function to process the flow. Make two SMI calls one by one in frontend and chain them with the help of promise because the second SMI call is dependent on the first call response. Make sure that your API needs to be responded within 5 secs.
For increasing the time execution of SMI, the Freshworks Marketplace team may help you in this.

4 Likes

Okay. @Sheik, @velmurugan, thank you both very much for your time and assistance. I greatly appreciate it!

3 Likes

This might be a little late, but I hope it isn’t @Dan_Herr.

Given that you seemed to be suggesting you are using the SMI feature to protect API keys, have you considered using the Request API feature instead? You will note that the first example there shows how to make an API call by referencing an installation parameter. Installation parameters can be secured using the secure flag, as listed in the configuration section.

This way, you can simply expect that the API keys are entered by a trusted admin when the app is installed, and then your app can simply reference them when making 3rd party API calls. Each API call will still need to complete within 5s, but you won’t need to use SMI anymore.

Hope this helps make your app leaner!

2 Likes

The error message “Can’t set headers after they are sent” commonly occurs in Node.js applications when attempting to modify response headers after the response has already been sent to the client. This error typically indicates a logic issue where multiple responses are being sent or headers are being set too late in the code execution. To resolve this error, ensure that response headers are set before sending the response and that only a single response is sent per request, avoiding any modifications to headers after the response has been sent.

If you’re using middleware in your application, particularly ones that modify the response headers, ensure that they are properly ordered. Middleware functions are executed in the order they are defined, so if a response is sent before a middleware function attempts to modify headers, the error can occur. Make sure the middleware that sets headers is placed before the middleware that sends the response. Also, When sending a response, ensure that you properly exit from the function or block of code. Use return statements or appropriate flow control mechanisms to prevent subsequent execution that may attempt to modify headers.