How to resolve Node.js error with Axios 0.21.4 resulting in a "Request failed with status code 400"?

This topic was automatically generated from Slack. You can find the original thread here.

No matter how I revise the node.js, I keep getting the same error. I am at a road block with this. keeps giving me the same response over and over however, ChatGPT suggests the following: “The error you are experiencing might be related to the version of Axios you are using. According to a discussion on GitHub, the issue you’re encountering seems to have been reported with axios@0.21.4, and it appears to be resolved in v0.22.0 or higher versions1. Please try upgrading Axios to the latest version and see if the problem persists. If you’re using npm, you can upgrade Axios by running” @UMT4G7E5P any thoughts?

Error - Request failed with status code 400
{“statusCode”:400,“message”:“Unexpected token " in JSON at position 0”,“error”:“Bad Request”}
DETAILS

    at null.createError (/tmp/__pdg__/dist/code/3cd7d6d2de872d25a780682b808d756a6dbc95552bf467b1abba5672e9b54e8d/node_modules/.pnpm/axios@0.21.4/node_modules/axios/lib/core/createError.js:16:15)
    at null.settle (/tmp/__pdg__/dist/code/3cd7d6d2de872d25a780682b808d756a6dbc95552bf467b1abba5672e9b54e8d/node_modules/.pnpm/axios@0.21.4/node_modules/axios/lib/core/settle.js:17:12)
    at IncomingMessage.handleStreamEnd (/tmp/__pdg__/dist/code/3cd7d6d2de872d25a780682b808d756a6dbc95552bf467b1abba5672e9b54e8d/node_modules/.pnpm/axios@0.21.4/node_modules/axios/lib/adapters/http.js:269:11)
    at IncomingMessage.emit (events.js:412:35)
    at null.endReadableNT (internal/streams/readable.js:1333:12)
    at process.processTicksAndRejections (internal/process/task_queues.js:82:21)

steps.gohighlevel_1{1}
debug{5}
status:
400
statusText:
Bad Request
headers{10}
x-powered-by:
Express
access-control-allow-origin:
**
content-type:
application/json; charset=utf-8
content-length:
94
etag:
W/"5e-ZkBjh4z5cM88ZRQ74a5hde5GNS8"
vary:
Accept-Encoding
date:
Fri, 26 May 2023 21:11:07 GMT
via:
1.1 google
alt-svc:
h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
connection:
close
config{12}
url
https://services.leadconnectorhq.com/locations/ck6TDBskjrhSPWEO92xX/customFields/
method:
put
data"Hello James, welcome to the :hammer_and_pick: LeadForge OS :hammer_and_pick: demo! I’m excited to show you how we can help Miami Yachts convert marketing leads into sales-ready leads with our powerful AI-driven system.\n\nJust provide me with the Marketing Qualified Lead (MQL) details, including their name, problem/goal they want to achieve, any specific details around their problem/goal, and how soon they want to achieve their goal or resolve their problem.\n\nOnce you share the MQL details, I’ll start the demonstration, engaging with you as if you were the lead. When we’re done with the demo, you can run a valuable :hammer_and_pick:LeadSmith Lead Report by typing /lead report to get insights on lead performance and the conversion process."headers{6}
Accept:
application/json, text/plain, **/*
Content-Type:
application/json
Authorization:
Bearer a245d8cdb0e965eb82db143ac10ae22187dcc4ed
Version:
2021-07-28
User-Agent:
axios/0.21.4
Content-Length:
719
transformRequest[1]
0:
null
transformResponse[1]
0:
null
timeout:
0
xsrfCookieName:
XSRF-TOKEN
xsrfHeaderName:
X-XSRF-TOKEN
maxContentLength:
-1
-more-
data{3}
statusCode:
400
message:
Unexpected token " in JSON at position 0
error:
Bad Request

I’m sorry to hear that you’re experiencing issues with your code. Based on the error message you provided, it seems like there might be an issue with the JSON data you’re sending in your request. The error message “Unexpected token " in JSON at position 0” indicates that there might be an issue with the formatting of the JSON data. To help you further, I would need to see the actual code you’re using to make the request. Could you please share the relevant part of your code, especially the part where you’re making the HTTP request and passing the JSON data? This will help me understand the issue better and provide a more accurate solution.

, here’s the code: import { axios } from@pipedream/platform”;

export default defineComponent({
props: {
locationId: {
type: “string”,
label: “Location ID”,
},
customFieldId: {
type: “string”,
label: “Custom Field ID”,
},
updateData: {
type: “object”,
label: “Update Data”,
},
authorization: {
type: “string”,
label: “Authorization Token”,
},
},
async run({ steps, $ }) {
const baseURL = “https://services.leadconnectorhq.com”;
const headers = {
Authorization: Bearer ${**this**.authorization},
Version: “2021-07-28”,
“Content-Type”: “application/json”,
};

**const** getCustomFields = **async** (locationId) => {
  **const** endpoint = `/locations/${locationId}/customFields`;

  console.log("getCustomFields request data:", {
    method: "GET",
    url: baseURL + endpoint,
    headers: headers,
  });

  **const** response = **await** axios($, {
    method: "GET",
    url: baseURL + endpoint,
    headers: headers,
  });

  **if** (response.status === 200) {
    **return** response.data.customFields;
  } **else** {
    **return** **null**;
  }
};

**const** updateCustomField = **async** (locationId, customFieldId, data) => {
  **const** endpoint = `/locations/${locationId}/customFields/${customFieldId}`;

  console.log("updateCustomField request data:", {
    method: "PUT",
    url: baseURL + endpoint,
    headers: headers,
    data: data,
  });

  _// If `data` is not already a string, stringify it_
  **const** requestData = **typeof** data === "string" ? data : JSON.stringify(data);

  **const** response = **await** axios($, {
    method: "PUT",
    url: baseURL + endpoint,
    headers: headers,
    data: requestData,
  });

  **if** (response.status === 200) {
    **return** response.data.customField;
  } **else** {
    **return** **null**;
  }
};

**const** customFields = **await** getCustomFields(**this**.locationId);
console.log("custom_fields", customFields);

**const** updatedCustomField = **await** updateCustomField(
  **this**.locationId,
  **this**.customFieldId,
  **this**.updateData
);
console.log("updated_custom_field", updatedCustomField);

**return** {
  customFields: customFields,
  updatedCustomField: updatedCustomField,
};

},
});

Can you show me exactly what data you’re passing to that field? It suggests you might already have a string that you’re trying to pass as an object

Thank you for providing the code. I’ve reviewed it and it seems that you’re correctly using the axios package from @pipedream/platform and making the necessary API calls. Since the error message you’re encountering is related to the JSON data being sent in the request, I would recommend double-checking the updateData prop that you’re passing to the updateCustomField function. Make sure that the updateData object you’re passing to the component is properly formatted and doesn’t contain any unexpected characters or issues. You can try logging the updateData object before making the API call to see if there’s anything unusual: ```
console.log(“updateData:”, this.updateData);

Here you go: Loom | Free Screen & Video Recording Software | Loom

even when I changed it from object to string in the code, it still throws the same error.

Thanks for checking. Off hand I’m not sure what the exact issue is. We don’t support code troubleshooting directly (since it’s not a direct part of our integrations), but hopefully you can keep tracing the issue (and I’d recommend using the example Mario used above to log the data, and maybe also the type of the data, just to help pinpoint where the issue is - that’s how I’d troubleshoot myself)

I have used the log function however, I am unsure where this data will show in pipedream, as I am a new user.

No worries, happy to help with Pipedream issues like that. Take a look at the Logs tab in the step results

You can also call $.export(“variable name here”, variable) and that will export the data on the main Exports tab. That can often be easier to read

FYI, this is also using V2 for Go High Level since you guys are on V1. I don’t see that as a problem however, it is likely why Mario is unable to help

Yes for sure, that is still on our backlog and agreed that Mario should give better answers once that’s done

did you see that I offered a paid account for you guys to test on?

Yes, thanks. Just give us a bit of time to get to it, we have a large list of integrations we’re working through at any given time.

Understood