I am struggling to write an appscript to post data to sage accounting API
I am trying to save a new customer to sage accounting software through the API. When I use postman it works. I cant get the appscript to work though.
The following is the code I am using:
function postDataToSageOneAPI() { // Define your API endpoint URL var apiUrl = 'https://resellers.accounting.sageone.co.za/api/2.0.0/Customer/Save'; // Define authentication credentials var username = 'myusername'; var password = 'mypassword'; // Extract the CompanyId from the URL var companyId = "14787"; // Define the API key var apiKey = "{####################################}"; // Create the payload data you want to send as JSON var payload = '{"Name": "string1", "Email": "string2"}'; // Assuming this is a valid JSON string // Add the API key and CompanyId to the URL as query parameters apiUrl += '?apikey='+ apiKey +'&CompanyId='+ companyId; // Define the headers for the request var headers = {'Authorization': 'Basic '+ Utilities.base64Encode(username +':'+ password),'Content-Type': 'application/json', }; // Configure the options for the request var options = {'method': 'POST','headers': headers,'payload': payload, }; // Make the POST request to the Sage One API var response = UrlFetchApp.fetch(apiUrl, options); // Check the response status code var statusCode = response.getResponseCode(); // Handle the response if (statusCode === 200) { var responseBody = response.getContentText(); // Process the responseBody as needed Logger.log('Response Body: '+ responseBody); } else { // Handle errors or other status codes Logger.log('Error: '+ statusCode); }}
I keep getting the following error:
Exception: Invalid argument:
https://resellers.accounting.sageone.co.za/api/2.0.0/Customer/Save?apikey={#####################}&CompanyId=14787postDataToSageOneAPI @ Code.gs:35
What's the problem?