Module:SPARQL: Difference between revisions

From TITAF TANGOWIKI
(Created page with "local p = {} local http = require('mw.http') local json = require('mw.text').jsonDecode function p.runQuery(query) local endpoint = "https://dbpedia.org/sparql" -- Change if using a different SPARQL service local url = endpoint .. "?query=" .. mw.uri.encode(query) local response = http.request{ method = "GET", url = url, headers = { ["Accept"] = "application/json" } } if response.status ~= 200 then return "SPARQ...")
 
No edit summary
Line 1: Line 1:
local p = {}
local http = require("socket.http")


local http = require('mw.http')
local function fetch_url(url)
local json = require('mw.text').jsonDecode
     local body, code, headers = http.request(url)
 
     return body
function p.runQuery(query)
    local endpoint = "https://dbpedia.org/sparql" -- Change if using a different SPARQL service
    local url = endpoint .. "?query=" .. mw.uri.encode(query)
   
     local response = http.request{
        method = "GET",
        url = url,
        headers = { ["Accept"] = "application/json" }
     }
 
    if response.status ~= 200 then
        return "SPARQL query failed! Status: " .. response.status
    end
 
    return json(response.body)
end
end


return p
return fetch_url("https://www.example.com")

Revision as of 22:35, 21 March 2025

Documentation for this module may be created at Module:SPARQL/doc

local http = require("socket.http")

local function fetch_url(url)
    local body, code, headers = http.request(url)
    return body
end

return fetch_url("https://www.example.com")