user2538040
user2538040

Reputation: 31

Invalid Argument on basic API call using UrlFetchApp.fetch(url)

I'm new to Google Apps Scripts and I've been trying to make a simple Get call to a URL. I make this call from my browser: https://accounting.sageone.co.za/api/1.1.1/Supplier/Get?apikey={4CFEEFE1-CE04-425F-82C3-DCB179C817D5}&companyid=164740 and get the respons I'm looking for. I now try to make the call from Google Apps Scripts using the following code:

function myFunction() {
var url = 'https://accounting.sageone.co.za/api/1.1.1/Supplier/Get?apikey={4CFEEFE1-CE04-425F-82C3-DCB179C817D5}&companyid=164740
var response = UrlFetchApp.fetch(url);
Logger.log(response);
}'

I get a respons stating ' Message details Invalid argument: https://accounting.sageone.co.za/api/1.1.1/Supplier/Get?apikey={4CFEEFE1-CE04-425F-82C3-DCB179C817D5}&companyid=164740 (line 4, file "Code")'

I've tried a whole bunch of permutations with no luck...

Upvotes: 1

Views: 1657

Answers (1)

Douglas Gaskell
Douglas Gaskell

Reputation: 10030

When using UrlFetchApp, you need to enter your URL parameters as part of a request parameters rather than in the URL itself. For a GET request these go directy as part of the parameters, for a POST request the parameters would be part of a payload object. Reference Documentation

Here is a modified function:

function myFunction() {
  var url = 'https://accounting.sageone.co.za/api/1.1.1/Supplier/Get'  
  var params = {
    "method": 'GET',
    "apikey": "{4CFEEFE1-CE04-425F-82C3-DCB179C817D5}",
    "companyid": "164740"
  }
  var response = UrlFetchApp.fetch(url, params);
  Logger.log(response)
}

Note: This corrects your method, however, the server still requires further authentication. If you run into issues with that, ask another questions specific to that issue as well.

Upvotes: 5

Related Questions