Hubspot Client Libraries ERROR: Cannot parse content. No Content-Type defined

Hello,
I have developed a custom code snippet for a contact-based workflow that updates a record of a custom object associated with the contact. In this code I am using the Hubspot library, more specifically the update method, as shown in the following code:

const hubspot = require('@hubspot/api-client');

exports.main = async (event) => {
 const hubspotClient = new hubspot.Client({ accessToken: process.env.TOKEN_CUSTOM_OBJECTS });
 const current_object_id = event.inputFields['current_object_id']; 

 const new_properties = {
 properties: {
 hs_pipeline: 123456789,
 hs_pipeline_stage: 987654321,
 property_1: event.inputFields['property_1'],
 property_2: event.inputFields['property_2'],
 property_3: event.inputFields['property_3'],
 }
 }; 

 await hubspotClient.crm.objects.basicApi.update("2-########", current_object_id, new_properties, undefined);
}

Randomly, I receive an error notification in this custom code, containing the following response:

ERROR Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"Error: Cannot parse content. No Content-Type defined.","reason":{"errorType":"Error","errorMessage":"Cannot parse content. No Content-Type defined.","stack":["Error: Cannot parse content. No Content-Type defined."," at ObjectSerializer.parse (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/objects/models/ObjectSerializer.js:311:19)"," at BasicApiResponseProcessor.<anonymous> (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/objects/apis/BasicApi.js:282:114)"," at Generator.next (<anonymous>)"," at fulfilled (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/objects/apis/BasicApi.js:5:58)"," at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: Error: Cannot parse content. No Content-Type defined."," at process.<anonymous> (file:///var/runtime/index.mjs:1276:17)"," at process.emit (node:events:519:28)"," at emitUnhandledRejection (node:internal/process/promises:250:13)"," at throwUnhandledRejectionsMode (node:internal/process/promises:385:19)"," at processPromiseRejections (node:internal/process/promises:470:17)"," at process.processTicksAndRejections (node:internal/process/task_queues:96:32)"]}

These errors do not happen very often, but for some time now 2 to 3 cases have been appearing per day.

Could someone help me understand what might be happening?

Thanks,

Hey, @rocketman22 :waving_hand: Thanks for your question. I don’t have an answer to your issue, but I have a few suggestions. Have you tried:

  • Adding error handing with a try-catch block?
  • Explicitly setting your Content-Type header?
  • Adding retry logic

Something like:

const hubspot = require('@hubspot/api-client');

exports.main = async (event) => {
 const hubspotClient = new hubspot.Client({ accessToken: process.env.TOKEN_CUSTOM_OBJECTS });
 const current_object_id = event.inputFields['current_object_id']; 

 const new_properties = {
 properties: {
 hs_pipeline: 123456789,
 hs_pipeline_stage: 987654321,
 property_1: event.inputFields['property_1'],
 property_2: event.inputFields['property_2'],
 property_3: event.inputFields['property_3'],
 }
 }; 

 try {
 // Let's add retry logic
 let retries = 3;
 let success = false;
 let lastError;

 while (retries > 0 && !success) {
 try {
 await hubspotClient.crm.objects.basicApi.update(
 "2-########", 
 current_object_id, 
 new_properties,
 {
 headers: {
 'Content-Type': 'application/json'
 }
 }
 );
 success = true;
 } catch (error) {
 lastError = error;
 retries--;
 if (retries > 0) {
 // Pause for the cause. Wait for 1 second before retrying
 await new Promise(resolve => setTimeout(resolve, 1000));
 }
 }
 }

 if (!success) {
 throw lastError;
 }
 } catch (error) {
 // Log the error 
 console.error('Error updating HubSpot object:', {
 objectId: current_object_id,
 error: error.message,
 stack: error.stack
 });
 throw error; // Re-throw if you want the workflow to fail
 }
}

I hope this helps. Have fun testing! — Jaycee

Hi @Jaycee_Lewis,

Thank you very much for your reply!

I really liked your suggestions, they seem like good practices that I hadn’t considered.

I’m going to analyze and apply them to my code and see if the errors stop.

Cheers,