Problem with UrlFetchApp.fetch in Google Apps Script - google-apps-script
I am trying to use the ImportJSON.gs script to import data from my WooCommerce Web site directly into a Google Sheet. I have enabled the REST API in WooCommerce and am able to enter the URL directly into Chrome and get an accurate JSON response. The problem I am having is that when using UrlFetchApp.fetch, the data I get back suggests everything in the URL following a ? or & seems to be either truncated or reencoded to something else, but I haven't quite figured out what URL would give this response.
I have created this simple script to test it:
function myFunction() {
var response = UrlFetchApp.fetch("https://kidswonder.net/wp-json/wc-bookings/v1/products/slots?min_date=2023-01-30&max_date=2023-01-31&product_ids=16304");
Logger.log(response.getContentText());
}
The expected JSON response, which I get by entering the URL directly into a browser is:
{"records":[{"date":"2023-01-30T09:00","duration":30,"available":50,"booked":0,"product_id":16304},{"date":"2023-01-30T09:30","duration":30,"available":47,"booked":3,"product_id":16304},{"date":"2023-01-30T10:00","duration":30,"available":48,"booked":2,"product_id":16304},{"date":"2023-01-30T10:30","duration":30,"available":50,"booked":0,"product_id":16304},{"date":"2023-01-30T11:00","duration":30,"available":50,"booked":0,"product_id":16304},{"date":"2023-01-30T11:30","duration":30,"available":50,"booked":0,"product_id":16304},{"date":"2023-01-30T12:00","duration":30,"available":50,"booked":0,"product_id":16304},{"date":"2023-01-30T12:30","duration":30,"available":50,"booked":0,"product_id":16304},{"date":"2023-01-30T13:00","duration":30,"available":45,"booked":5,"product_id":16304},{"date":"2023-01-30T13:30","duration":30,"available":50,"booked":0,"product_id":16304},{"date":"2023-01-30T14:00","duration":30,"available":50,"booked":0,"product_id":16304},{"date":"2023-01-30T14:30","duration":30,"available":50,"booked":0,"product_id":16304},{"date":"2023-01-30T15:00","duration":30,"available":50,"booked":0,"product_id":16304},{"date":"2023-01-30T15:30","duration":30,"available":50,"booked":0,"product_id":16304}],"count":14}
But the actual response I get, which is the same as if I had is:
{"records":[{"date":"2023-02-02T09:00","duration":30,"available":1,"booked":0,"product_id":140},{"date":"2023-02-02T10:00","duration":30,"available":1,"booked":0,"product_id":140},{"date":"2023-02-02T14:00","duration":30,"available":1,"booked":0,"product_id":140},{"date":"2023-02-02T15:00","duration":30,"available":1,"booked":0,"product_id":140},{"date":"2023-02-05T17:00","duration":30,"available":1,"booked":0,"product_id":12314},{"date":"2023-02-05T17:00","duration":30,"available":1,"booked":0,"product_id":12312},{"date":"2023-02-06T10:00","duration":30,"available":1,"booked":0,"product_id":103024},{"date":"2023-02-06T10:00","duration":30,"available":1,"booked":0,"product_id":12312},{"date":"2023-02-06T11:00","duration":30,"available":1,"booked":0,"product_id":103024},{"date":"2023-02-06T11:00","duration":30,"available":1,"booked":0,"product_id":12312},{"date":"2023-02-06T12:00","duration":30,"available":1,"booked":0,"product_id":103024},{"date":"2023-02-06T12:00","duration":30,"available":1,"booked":0,"product_id":12312},{"date":"2023-02-06T13:00","duration":30,"available":1,"booked":0,"product_id":103024},{"date":"2023-02-06T13:00","duration":30,"available":1,"booked":0,"product_id":12312},{"date":"2023-02-06T14:00","duration":30,"available":1,"booked":0,"product_id":103024},{"date":"2023-02-06T14:00","duration":30,"available":1,"booked":0,"product_id":12312},{"date":"2023-02-06T15:00","duration":30,"available":1,"booked":0,"product_id":103024},{"date":"2023-02-06T15:00","duration":30,"available":1,"booked":0,"product_id":12312}],"count":18}
Related
API Call In Google Sheets Google Apps Script Returning Code 500
I am trying to call cutt.ly's api for shortening urls. When pasting the cal into safari I get the desired response; however, when I make the call using URLFetch in my Google Apps Script Project (making for a Google Sheets Add On) I only get a code of 500. I have tried the following url in Safari and it has returned the desired information: https://cutt.ly/api/api.php?key=[APIKEY]&short=https://www.ThisIsMyReallyLongLink.com&name=MyTestLinkThatIsUnused However, if I make the call in Google Apps Script like so, I only get code 500: UrlFetchApp.fetch("https://cutt.ly/api/api.php?key=[APIKEY]&short=https://www.ThisIsMyReallyLongLink.com&name=MyTestLinkThatIsUnused"); The specific error is: "Exception: Request failed for https://cutt.ly/api/api.php?key=[APIKEY]&short=https://www.ThisIsMyReallyLongLink.com&name=MyTestLinkThatIsUnused returned code 500" Documentation for the cutt.ly API is located here https://cutt.ly/cuttly-api Does anyone have any thoughts on why I am only getting this error in Google Apps Script and how to potentially resolve. If possible I want to avoid doing something like this UrlFetchApp.fetch(url, {"muteHttpExceptions": true}) to make sure that I catch all exceptions and handle properly. function test(){ var response = UrlFetchApp.fetch("cutt.ly/api/api.php?key=[APIKEY]&short=https://…); Logger.log(response); }
Matching response to request using UrlFetchApp.fetchAll
Im trying to validate bunch of urls that are stored in google spreadsheet using Google App Script. Since there are many urls to validate and UrlFetchApp.fetch does not execute http request in parallel, it becomes slow. So I would like to use UrlFetchApp.fetchAll, which works asynchronously. But, there is problem: var responses = UrlFetchApp.fetchAll(["http://www.bar.com", "http://www.foo.com"]); responses[0] // Is this response from http://www.bar.com or http://www.foo.com responses[1] // Is this response from http://www.bar.com or http://www.foo.com Now responses is array of HTTPResponse objects. But to give user feedback, I would need to indicate responses that fail. But problem is that in response, there is no reference to request or URL. So problem is that I have array of responses but I'm unable to find URL this response came from. Responses array is not in same order as given to argument.
How to get parameters that are starting with hash character in Google Apps Script?
I'm trying to auth on third party webservice. This service sending to my google web app request looks something like https://script.google.com/...exec#myvar1=1&myvar2=2 I get request in doGet function doGet(e) { writeData(JSON.stringify(e)); } and I don't see anything containing myvar1 in object e: {"parameter":{},"contextPath":"","contentLength":-1,"queryString":"","parameters":{}} But if I change # to ? in url, https://script.google.com/...exec#myvar1=1&myvar2=2 I'll predictably see myvar1 in parameter property. Unfortunately, the url is formatted by the third party service, so I can't change it.
UrlFetchApp doGet not called and return html with "Meet Google Drive"
I must be losing my mind as suddenly I can't get a simple web app to work. When I use the URL generated in the browser everything works fine. When I run the script the result is HTML content with Meet Google Drive. Here is the code: function doGet(e) { return ContentService.createTextOutput('blabla').setMimeType(ContentService.MimeType.TEXT); } function client() { var url = 'https://script.google.com/macros/s/AKfycby2NVrhG0O5fE6gTgage2QPYH3UJ2s23AJDLnB9YL69uyDFlmM/exec'; Logger.log(url); var result = UrlFetchApp.fetch(url); Logger.log(result); } Both doGet and the client functions are at the same file which was never a problem. A version was saved and the app deployed as anyone accessing the app and run as user accessing the app. result.getContentText() returns the same html output. result.getResponseCode() returns 200 which supposed to be successful however I can't see the doGet being called.
I had the same issue. When you deploy the WebApp, make sure the authorizations are set to "Anyone, including Anonymous"!
Google Spreadsheet JSON request - 400 status
Hi I have a public google spreadsheet at Google Docs. However when i request the JSON with: $.getJSON( "https://spreadsheets.google.com/feeds/list/0Ak0qDiMLT3XddHlNempadUs1djdkQ0tFLWF6ci1rUUE/od6/public/values?alt=json-in-script&callback=?&gid=0", function (data) { console.log(data) }) I get a HTTP 400 request error. Strangely the same exact code works for this spreadsheet. What's going on here? Thanks a lot.
I was looking for a solution to the same problem. It led me to this. Just want to share the fix: "Warning: API requests using the public visiblity for feeds on spreadsheets that are not "Published to the Web" yield an HTTP 400 Bad Request response with the message The spreadsheet at this URL could not be found. Make sure that you have the right URL and that the owner of the spreadsheet hasn't deleted it."
From my experience, I guess you maybe had forgotten to share it or forgotten to make it public. I just tried it and it is working fine. By entering this link in a browser: https://spreadsheets.google.com/feeds/list/0Ak0qDiMLT3XddHlNempadUs1djdkQ0tFLWF6ci1rUUE/od6/public/values?alt=json-in-script&gid=0