Escape curly braces in Google Script

1.2k views Asked by At

Apologies for any lack of technical specificity in this post, I’m stumbling around in the dark at the moment.

I’m currently trying to write a Google script to call the Insightly API (https://api.insight.ly) and I’ve run into a problem.

As far as I can see Insightly requires curly braces to be used when referring to a record’s ID in an API call, however Google script returns an error when URL’s containing curly braces are used.

How can I escape those curly braces so Google script treats them the same as any other bit of text?

Here’s my code

var url = "https://api.insight.ly/v2/Opportunities/{217}"
var response = UrlFetchApp.fetch(url, headers);

Thanks :)

2

There are 2 answers

3
Pointy On BEST ANSWER

Try

var url = "https://api.insight.ly/v2/Opportunities/" + encodeURIComponent("{217}");

The { must be encoded as %7B and } as %7D. You can hard-code those if you like, but it's nice to be able to see the actual string in the source code.

0
Dylan On

A bit late but.. Hope it helps.. You do not have to put the { and }.. the right code is

var url = "https://api.insight.ly/v2/Opportunities/217"