Surprising glitch encountered while using UrlFetchApp.fetch in Google Apps Script

My current task involves accessing the Pingdom API using Google Apps Script, following this example provided: https://developers.google.com/apps-script/external_apis

query = 'credits';
var username = 'foo';
var password = 'bar';
var credentials = username+':'+password;
var url = 'https://'+credentials+'@api.pingdom.com/api/2.0/'+encodeURIComponent(query);
var headers = {
  "App-Key": "abcd",
};
var options = {
    "method": "get",
    "headers": headers,
    'validateHttpsCertificates':false
};
Logger.log(url);
var response = UrlFetchApp.fetch(url);

However, I encounter an error during code execution:

Unexpected error: https://foo:[email protected]/api/2.0/credits (line 17, file "Code") Dismiss

When I manually paste the URL above into a browser, it works fine (generating a "Missing application key" message from the Pingdom API, indicating that the username and password were correctly provided). I've tried with and without using encodeURIComponent on credentials, but the same error persists. Even adding 'muteHttpExceptions':true does not resolve the issue.

Any insights on what might be causing this error?

Answer №1

There seems to be a problem when login details are included in the URL, causing an error with UrlFetchApp. We recommend reporting this issue on our Bug Tracker. In the meantime, it is advised to place the login information within the Authorization header for proper functionality.

var response = UrlFetchApp.fetch(url, {
  headers: {
    'Authorization': 'Basic ' + Utilities.base64Encode(username + ':' + password)
  }
});

Answer №2

When attempting to use the Gitlab API within Google Apps Script, I encountered similar challenges as others have mentioned. The method of including login details in the URL resulted in errors, and the Basic Authorization approach also did not prove successful for me. However, adapting the authorization header to use the 'Bearer' type resolved the issue.

const gitlabUrl = "https://gitlab.com/api/v4/projects/" + projectId;
const requestHeaders = {
  'headers' : {Authorization: 'Bearer ' + token}
};
const apiResponse = UrlFetchApp.fetch(gitlabUrl, requestHeaders);

Answer №3

My approach to solving a similar issue was quite straightforward:

  var result = UrlFetchApp.fetch(downloadLink, settings);
  var document = result.getBlob();

The problematic line causing the error message was:

Error: Request made for https://docs.google.com resulted in a 500 error code. Server response was cut off: <meta name="viewport" c... (enable muteHttpExceptions option to view entire reply) funcNamePdfAndRep @ PDF and Report Function.gs:39

This issue occurred because the sheets that had been used to create the PDF were hidden, just as a precaution.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Leverage the power of Meteor by incorporating templates to dynamically inject HTML based on an event

I am currently generating HTML using JQuery on an event. However, I believe a better approach would be to use templates in Meteor, especially if the HTML becomes more complex. Template.example.onRendered(function() { paper.on({ 'cell:mous ...

Generate nth-child selectors in a Material-UI component using props dynamically

I am currently working on customizing the Material UI slider component, specifically focusing on its marks prop to display the number of occurrences for each data object within the marks array. The desired appearance of the slider is illustrated in this i ...

Determine the ratio of the background image

I am facing an issue with layering background-images on my webpage. I have a background-image named 1 at the bottom of the page, and I want to display another div with a background-image named 2 over the bottom 5% of background-image 1 (not the bottom 30% ...

Search for all fields using Mongoose with the $regex operator

Imagine having a model like this: var advertisement = mongoose.Schema({ username: String, text: String, locations: Array, days: Array, phone: Object, images: Array, age: Number, height: String, sex: String, cate ...

Creating Nested Arrays in Angular: A Step-by-Step Guide

I'm struggling to create an array of arrays and I can't seem to figure out what's wrong with my code. Here's the snippet: c.push({ id: data[0]["id"], title:data[v]["title"], date:data[v]["date"], text:data[v]["text"], favorit: ...

jQuery: What is the reason this small function is not functioning as expected?

How can I add a number to the right of LI elements in sequence? Is there any difference between i = i+1; and i++;? If so, what is it? Check out this link for more information: http://jsfiddle.net/nicktheandroid/U8byW/9/ ...

Navigating between Vue Router pages triggers multiple events within the mounted() lifecycle of VueJS

Currently, I am immersed in a project using Electron with a Vue CLI setup and the Vue CLI Plugin Electron Builder. The overall functionality is working perfectly fine except for a peculiar bug that has recently surfaced. The issue arises when navigating b ...

Generating a File that Imports Components and Instantly Exports Them Once More

Having trouble with this code. Initially, I had: // file 1 import Box from '@/components/Box' import Popup from '@/components/Popup' const MDXComponents = { Box, Popup } // using MDXComponents somewhere in file 1 Now, to manage t ...

Is it possible to compare two arrays of objects in Angular 7 when one property is an array as well?

I am facing a challenge with two arrays in my code. The first array is a jobs array of objects that contains an array itemIds. Here is an example: jobs: [{ name: null id: 612 items: [] stat: 1 itemIds: [223, 1234] ...

How can Vue detect modifications made in the edited field?

I am facing an issue with tracking changes in a specific object. Here is the structure of the object: users: { email: '', password: '' } My goal is to detect any edits made to the keys within the users object and store the key ...

Is it possible to set a read-only attribute using getElementByName method

To make a text field "readonly" by placing JavaScript code inside an external JS file, the HTML page cannot be directly modified because it is located on a remote server. However, interaction can be done by adding code in an external JS file hosted on a pe ...

Converting XML to JSON in a Node.js application

I recently read an article on that explained the conversion process clearly, but unfortunately it's not working for me. Let me provide you with the code snippet: function parseXml(xml) { var dom = null; if (window.DOMParser) { try ...

Circular arrangement using D3 Circle Pack Layout in a horizontal orientation

I'm currently experimenting with creating a wordcloud using the D3 pack layout in a horizontal format. Instead of restricting the width, I am limiting the height for my layout. The pack layout automatically arranges the circles with the largest one ...

Does anyone know of a convenient tool that can automatically provide suggested street names for mailing addresses in every country across the globe, for free?

Is there a convenient tool that automatically completes street addresses for various countries worldwide? I'm considering options similar to YQL from Yahoo or Foursquare. I'd like to provide users with suggestions of known street names as they ...

Utilize a Chrome Content Script to intercept jQuery delegated event handlers and take control

After developing a Chrome extension that intercepts form submissions in specific circumstances, I encountered an issue with a particular website utilizing jQuery's delegate function. My extension is built with raw JavaScript, excluding jQuery to prev ...

Finding the actual path in any scenario using JavaScript ($.ajax)

Here is the structure of my project: Sil -> css -> js -> clients -> index.php -> response.php index.php I am facing an issue related to the folder 'clients'. My website URL can have different va ...

Assign the src value from JavaScript to the img tag

I need assistance on how to properly insert the image source I am receiving into the <img/> tag. JavaScript: var aa = document.getElementById("TableImage"+getid).src; alert(aa); Result of the above JavaScript: Now, I would like to place this link ...

I have a task that requires me to click on the 'OK' button within an ALERT pop-up while using Selenium on Internet Explorer

Clicking on the 'OK' button from an alert on Internet Explorer seems to be giving me trouble. I've attempted using drive.switch_to.alert().accept()/.send_keys(Keys.ENTER) with Selenium Webdriver in Python, but it doesn't seem to be work ...

What is the best way to break a single line into multiple lines when working in VS code?

I have written the following code in nodeJS.. async function GetSellerInfoByID({seller_user_id}) { console.debug("Method : GetSellerInfoByID @user.service.js Calling..."); await clientDB.connect(); dataBaseData = await clientDB. ...

Steps for establishing a connection to a MongoDB database on Heroku using mongoose

Everything is running smoothly with my app locally and it can successfully connect to the local database. However, when I attempt to run it on Heroku, I encounter the following error: 2014-04-17T06:32:23.404458+00:00 app[web.1]: > <a href="/cdn-cgi ...