Reputation: 437
My code is supposed to get some data from google sheet and POST to a system via external API.I, however, keep running into the error 404 when I run the code.
On debug, it indicates that response is not defined. Here is the potion of the code with the issue. Am I missing something?
function postLeave(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// used getLastRow() function [1] to narrow the array to have only cells with data.
var range = sheet.getRange("K2:K"+sheet.getLastRow()).getValues();
var searchString = "";
for (var i = 0; i<range.length; i++) {
if(range[i][0] == searchString) {
var lastRow = sheet.getRange(2+i,1,1,10).getValues();
var userid = sheet.getRange("I2:I" + sheet.getLastRow()).getValues();
var data = {
//'user_id': lastRow[0][8],
"leave_type_id":lastRow[0][9],
"date":lastRow[0][7],
"hours":lastRow[0][6],
};
var payload = JSON.stringify(data);
var options = {
'method': 'POST',
'Content-Type': 'application/json',
'payload' : data,
};
var url = 'https://api.10000ft.com/api/v1/users/' + userid + '/time_entries?auth=key;
var response = UrlFetchApp.fetch(url).getContentText();
if (response === 200) {
//var json = JSON.parse(response);
sheet.getRange(2+i, 11).setValue(1);
}
else {
sheet.getRange(2+i, 11).setValue(0);
Logger.log(response)
}
}
}
}
Documentation
Challenge with endpoints.
https://github.com/10Kft/10kft-api/blob/master/sections/assignables.md
https://github.com/10Kft/10kft-api/blob/master/sections/leave-types.md https://github.com/10Kft/10kft-api/blob/master/sections/time-entries.md
Upvotes: 1
Views: 1504
Reputation: 201388
How about this modification?
Content-Type
cannot be directly used to the option of UrlFetchApp.fetch()
.var payload = JSON.stringify(data);
is not used.options
is not used at var response = UrlFetchApp.fetch(url).getContentText();
. In this case, it requests to the URL with the GET method.var url = 'https://api.10000ft.com/api/v1/users/' + userid + '/time_entries?auth=key;
is not enclosed by '
.Unfortunately, from the official document, I couldn't understand if it is required to send this data in json data or form data. So I proposed following 2 patterns. Please check them.
In this modified script, the data is sent as json data. Please set each variables. And also please set key
.
var payload = JSON.stringify(data);
var options = {
'method': 'POST',
'Content-Type': 'application/json',
'payload' : data,
};
var url = 'https://api.10000ft.com/api/v1/users/' + userid + '/time_entries?auth=key;
var response = UrlFetchApp.fetch(url).getContentText();
To:
var payload = JSON.stringify(data);
var options = {
'method': 'POST',
'contentType': 'application/json',
'payload' : payload,
};
var key = "###"; // <--- Please set your key.
var url = 'https://api.10000ft.com/api/v1/users/' + userid + '/time_entries?auth=' + encodeURIComponent(key);
var response = UrlFetchApp.fetch(url, options).getContentText();
In this modified script, the data is sent as form data. Please set each variables. And also please set key
.
var payload = JSON.stringify(data);
var options = {
'method': 'POST',
'Content-Type': 'application/json',
'payload' : data,
};
var url = 'https://api.10000ft.com/api/v1/users/' + userid + '/time_entries?auth=key;
var response = UrlFetchApp.fetch(url).getContentText();
To:
var options = {
'method': 'POST',
'payload' : data,
};
var key = "###"; // <--- Please set your key.
var url = 'https://api.10000ft.com/api/v1/users/' + userid + '/time_entries?auth=' + encodeURIComponent(key);
var response = UrlFetchApp.fetch(url, options).getContentText();
data
object and key
are correct.Upvotes: 1