APIs & Integrations

PAtlan
Contributor

Parsing Axios response from an API call

I'm trying to get some information from a contact in custom code for a workflow.

I've set up a local environment to test my code. 

If I use fetch, I can access my HubSpot portal and get the information I need.

If I use the same route with Axios, the data I get is gibberish.

Here's a code sample:

 

 

const axios = require('axios')

const objectType = "contact"
const toObjectType = "site"
const route = `/crm/v3/objects/${objectType}/${contactID}/associations/${toObjectType}`;
axios.defaults.baseURL = 'https://api.hubapi.com';
axios.defaults.headers.common['Authorization'] = 'Bearer XXXXXXXXXXXXXXXXXXXXXXXX';


let config = {
  method: 'get',
  url: '/crm/v3/objects/contact/474379/associations/site',
};

axios(config)
.then(function (response) {
  console.log(response.data)
  let ctype = response.headers["content-type"];
  console.log(ctype);
})
.catch(function (error) {
  console.log(error);
});

 

 

 

This returns : 

 

 

!� �RY�0~�f����/7��@V�9������zBq1{�,�����ή/�Pn�
application/json;charset=utf-8

 

 

 

The same route, with the same Authorization, using fetch, returns:

 

 

{ results: [ { id: '491582160', type: 'site_to_contact' } ] }

 

 

 

I'm really not sure what I'm doing wrong. And because I can't seem to use fetch in a Hubspot Workflow code block, I really need to figure it out.

Thanks !

0 Upvotes
5 Replies 5
coldrickjack
Guide

Parsing Axios response from an API call

Hey @PAtlan,

 

I can't seem to replicate what you are seeing. For instance with the below code in a custom coded workflow action where 2-106192594 is the custom object schema ID for "Orders" in my test portal:

 

const axios = require('axios')

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

axios.defaults.baseURL = 'https://api.hubapi.com';
axios.defaults.headers.common['Authorization'] = 'Bearer ' + process.env.ACCESS_TOKEN;

let config = {
  method: 'get',
  url: '/crm/v3/objects/contact/151/associations/2-106182594'
};

axios(config)
.then(function (response) {
  console.log(response.data)
  let ctype = response.headers["content-type"];
  console.log(ctype);
})
.catch(function (error) {
  console.log(error);
});
}

 

I get the following response:

 

results: [
    { id: '267226578', type: 'order_to_contact' },
    { id: '267234515', type: 'order_to_contact' }
  ]

 

Also can you link to the documentation for the endpoint you have included within your example? I'm not able to find it on the developer docs.

 

LeeBartelme
HubSpot Employee
HubSpot Employee

Parsing Axios response from an API call

Try setting the "Accept" headers to "application/json".

0 Upvotes
PAtlan
Contributor

Parsing Axios response from an API call

Thanks for your reply, but no joy.

0 Upvotes
LeeBartelme
HubSpot Employee
HubSpot Employee

Parsing Axios response from an API call

Did you try running this code in an isolated workflow action without other code or logic, or is your provided example part of a larger code block? On the surface there really isn't any reason your result should be coming back that way as far as I can tell.

 

What's odd to me is "response.data" should contain a large JSON object that isn't unique to HubSpot. It should be Axios' standard response object. So your code resulting in what you posted tells me either there is some other logic outside of what you shared acting on the Axios object, or HubSpot was/is having some underlying issue when you were working on this. I don't see how it would have anything to do with the API itself.

Jaycee_Lewis
Community Manager
Community Manager

Parsing Axios response from an API call

Hi, @PAtlan 👋 Thanks for the question. Hey, @LMartinez7 @LeeBartelme, do you have any suggestions for @PAtlan?

 

Best,

Jaycee


HubSpot’s AI-powered customer agent resolves up to 50% of customer queries instantly, with some customers reaching up to 90% resolution rates.
Learn More.


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !
0 Upvotes