APIs & Integrations

GJakobTriad
Participant

Bad Request with simple, bodyless GET request

SOLVE

Trying to come up with a workflow custom code action, and this one starts off pretty simple, but for some reason it returns a 400 error.

 

 

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

exports.main = async (event, callback) => {

  // Make sure to add your API key under "Secrets" above.
  const hubspotClient = new hubspot.Client({
    accessToken: process.env.TicketConvoControl
  });

  const searchResults = await hubspotClient
    .apiRequest({
      method: 'GET',
      headers: {
        authorization: `Bearer ${process.env.TicketConvoControl}`
      },
      path: `/crm/v4/objects/tickets/${event.object.objectId}/associations/conversations?limit=1`
  });

// (code continues, but this first request fails)

 

 

The first thing I did before even getting to the custom code action is test on Postman with a simple cURL, which works without issue.

 

curl --location 'https://api.hubapi.com/crm/v4/objects/tickets/1234567890/associations/conversations?limit=1' \
--header 'Authorization: Bearer EXAMPLETOKEN'
1 Accepted solution
GJakobTriad
Solution
Participant

Bad Request with simple, bodyless GET request

SOLVE

Actually the issue was that I was sending the authentication twice.

 

First one was implied here:

 

const hubspotClient = new hubspot.Client({
    accessToken: process.env.TicketConvoControl
  });

 

So sending it as a header for searchResults was redundant and caused the 400 error.

View solution in original post

2 Replies 2
GJakobTriad
Solution
Participant

Bad Request with simple, bodyless GET request

SOLVE

Actually the issue was that I was sending the authentication twice.

 

First one was implied here:

 

const hubspotClient = new hubspot.Client({
    accessToken: process.env.TicketConvoControl
  });

 

So sending it as a header for searchResults was redundant and caused the 400 error.

GRajput
Key Advisor | Platinum Partner
Key Advisor | Platinum Partner

Bad Request with simple, bodyless GET request

SOLVE

Hi @GJakobTriad 

Please try with the below code

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

exports.main = async (event, callback) => {

  // Make sure to add your API key under "Secrets" above.
  const hubspotClient = new hubspot.Client({
    accessToken: process.env.TicketConvoControl
  });

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

const hubspotClient = new hubspot.Client({"accessToken":"YOUR_ACCESS_TOKEN"});

const objectType = "tickets";
const objectId = event.object.objectId;
const toObjectType = "conversations";
const after = undefined;
const limit = 1;

try {
  const apiResponse = await hubspotClient.crm.associations.v4.basicApi.getPage(objectType, objectId, toObjectType, after, limit);
  console.log(JSON.stringify(apiResponse, null, 2));
} catch (e) {
  e.message === 'HTTP request failed'
    ? console.error(JSON.stringify(e.response, null, 2))
    : console.error(e)
}
// (code continues, but this first request fails)

 

"I hope this will help you out. Please mark it as Solution Accepted and upvote to help another Community member.
Thanks!"




Gaurav Rajput
Director, MarTech( Growth Natives)

Book a meeting