google-apps-scriptwikidata-query-service

UrlFetchApp returns no data from Wikidata SPARQL service


I'm trying to use Google Apps Script contained in a sheet to return XML from the Wikidata web service for SPARQL queries. I have a simple working SPARQL query and am using the URL-escaped https string, per the instructions in the Wikidata user guide.

Per the code below, I've tried UrlFetchApp.Fetch() both with and without the options parameter, but I always get an empty response. When I test the "simpleQry" url in my browser, it returns the correct xml file. I don't use web services very often, so I may be missing something obvious. Any help is greatly appreciated!

First I set the fetch options with JSON

 //set http header values w/json
var headr = {
    'Accept': 'application/sparql-results+xml'
};

//set options values w/json
var options = {
    'method': 'get',
    'header': headr
};

Then I submit the request:

simpleQry = "https://query.wikidata.org/sparql?query=select%20%3FpersonLabel%20%3FemployerLabel%20%3FworksLabel%0Awhere%20%7B%0A%20%20VALUES%20%3Fperson%20%7Bwd%3AQ112129152%7D%20.%0A%20%20%3Fperson%20wdt%3AP108%20%3Femployer%20.%0A%20%20%3Fperson%20wdt%3AP800%20%3Fworks%20.%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20'%5BAUTO_LANGUAGE%5D%2Cen'.%20%7D%0A%20%20%7D";

var response = UrlFetchApp.fetch(simpleQry, options);
var dataAllxml = XmlService.parse(response.getContentText());

Process the results. This where the error gets uncovered. Length of content = 0.

var rows = [], data;

for (i = 0; i < dataAllxml.length; i++) {
    data = dataAllxml[i];
    rows.push([data.personLabel, data.employerLabel, data.worksLabel]); //
}

Solution

  • Modification points:

    When this is reflected in your script, it becomes as follows.

    Modified script:

    function myFunction() {
      var headr = {
        'Accept': 'application/sparql-results+xml'
      };
      var options = {
        'method': 'get',
        'header': headr
      };
      var simpleQry = "https://query.wikidata.org/sparql?query=select%20%3FpersonLabel%20%3FemployerLabel%20%3FworksLabel%0Awhere%20%7B%0A%20%20VALUES%20%3Fperson%20%7Bwd%3AQ112129152%7D%20.%0A%20%20%3Fperson%20wdt%3AP108%20%3Femployer%20.%0A%20%20%3Fperson%20wdt%3AP800%20%3Fworks%20.%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20'%5BAUTO_LANGUAGE%5D%2Cen'.%20%7D%0A%20%20%7D";
      var response = UrlFetchApp.fetch(simpleQry, options);
      var dataAllxml = XmlService.parse(response.getContentText());
    
      // I modified below script.
      var root = dataAllxml.getRootElement();
      var ns = root.getNamespace();
      var rows = root.getChild("results", ns).getChildren().map(e => e.getChildren().map(f => f.getValue().trim()));
      console.log(rows);
    }
    

    Testing:

    When this modified script is run, the following result is obtained.

    [
      ["Ramona L. Pérez","San Diego State University","Good and bad death: exploring the perspectives of older Mexican Americans."],
      ["Ramona L. Pérez","San Diego State University","Diabetes Cultural Beliefs and Traditional Medicine Use Among Health Center Patients in Oaxaca, Mexico."],
      ["Ramona L. Pérez","San Diego State University","Exposure to smoking in soap operas and movies: smoking cessation and attempts to quit."]
    ]
    

    References: