Here’s a pattern of article based on your request:
Ethereum: URLFETCHAPP Request fails in menu functions but not in custom functions
As a programmer working with Google Apps script, you are probably not foreign to the challenge of connecting to the external API. Recently, I came across a problem in which my demands of Urlfetchapp" failed when trying to download market prices from external Rest API, but not when adapted functions are used.
Problem: External API Request
When I used the following code in the menu function:
Javascript
Function Getarmket Price () {{{
VAR Options = {
Method: ‘Get’,
Headers: {{
‘Content type’: ‘Application/Json’
Iche
};
VAR Response = urlfetchapp.fetch (‘ options);
VAR DATA = JSON.PARSE (Answer.Getcontenttext ());
return data;
Iche
`
The request failed with a message on the error indicating that the URL was malformed. After further investigation, I realized that the problem was not associated with the problem with the point of API, but with the wayUrlfetchapp ‘was configured.
SOLUTION OFFERED FUNCTIONS
When using custom functions, the problem may be more complex and requires carefully consideration of the HTTP request to solve.
After some research and experimentation, I discovered that if you make HTTP requirements from custom function, it is crucial to use the URL
instead ofUrlfetchapp
. In particular, when creating a new URL
object for an external API request:
`Javascript
Function Getarmket Price () {{{
VAR Options = {
Method: ‘Get’,
Headers: {{
‘Content type’: ‘Application/Json’
Iche
};
VAR URL = NEW URL (‘
Url.searchparam.Set (‘Symbol’, ‘Btcusdt’); // Replace the desired symbol
VAR Response = urlfetchapp.fetch (url.href, options);
VAR DATA = JSON.PARSE (Answer.Getcontenttext ());
return data;
Iche
`
The key difference between the use ofurlfetchapp ‘and the creation of a newURL
object is that the first uses the property of Href
resulting URL, which allows you to easily add the parameters of the query (such as symbols). This approach functions in both menu functions and adapted functions.
Conclusion
In conclusion, if you have problems with the external requirements of API when you use Urlfetchapp
, but not when you use custom functions, this is probably because of the way HTTP requirements are configured within your script. By crossing with Urlfetchapp
to the creation of a new objectURL
or by adding parameters of inquiries directly to the resulting URL, you should be able to solve the problem and successfully retrieve market prices from the external rest API.
I hope this article was helpful in solving the problem for you!