We use cookies to make HubSpot's community a better place. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. To learn more, and to see a full list of cookies we use, check out our Cookie Policy (baked goods not included).
Mar 18, 2021 8:26 AM
Hi!
Background:
We have developed a calculator in Google Sheets for our team to evaluate how much of work each of our initial offers to our clients would generate. We are populating this data from the Sheets to each Deal's Line Items. Problem is, that this is a manual task and it would be efficient to just update the values by using the API.
Information and the problem:
I have been using the API key with UrlFetchApp.fetch() using the url:
var url = "https://api.hubapi.com/crm/v3/objects/line_items/"+ lineItemID
+"?"+ hapikey;
var options = {
"method": "PATCH",
"headers": {
"content-type": "application/json"
},
"body":{
"properties": {
"quantity": "2",
"amount": "600.000"
}
},
//"muteHttpExceptions":true
}
UrlFetchApp.fetch(url,options);
When the mute is off it gives this error message: "Exception: Request failed for https://api.hubapi.com returned code 400. Truncated server response: {"status":"error","message":"Invalid JSON input: a request body was expected, but none found""
Most likely the reason is super simple but I just could not find anywhere an example of PATCH in JS that was not using oAuth but API-key.
Mar 24, 2021 8:18 AM - edited Mar 24, 2021 8:21 AM
Hi @JPs12
Looks like the solution is to send the JSON body as a string (via JSON.stringify() method), and to use the "payload" property name in place of where you've used "body" in your example. This should work within a Google Apps script function:
var url = "https://api.hubapi.com/crm/v3/objects/line_items/"+ LINE_ITEM_ID + "?hapikey=" + YOUR_HAPIKEY;
var data = {
"properties": {
"quantity": "2",
"amount": "600.000"
}
}
var options = {
"method": "patch",
"contentType": "application/json",
"payload": JSON.stringify(data)
}
UrlFetchApp.fetch(url,options);
I came to this conclusion after reading this piece of Google Apps Script documentation on the "UrlFetchApp" class.
Hope this helps.
All the best,
Zach
Mar 18, 2021 2:57 PM
Headers should probably include content-length of the body but I'd look at the docs for any other clues.
Mar 18, 2021 1:33 PM
Hi @JPs12
How is this coming along?
@zaklein , @malcolm1 see anything out of sorts here?
![]() | Make sure to subscribe to our YouTube channel where you can find the HubSpot Community Developer Show |