;; Example query to wikidata listing cats (use-modules (ice-9 receive) (ice-9 rdelim) (ice-9 textual-ports) (web response) (web client) (web uri)) (define q2 " SELECT ?item WHERE { ?item wdt:P31 wd:Q146. } LIMIT 10 ") (define post-url "https://query.wikidata.org/sparql") (define json "application/sparql-results+json") (define csv "text/csv") (define type csv) (define* (old-url-encoding input #:optional (index 0) (output "")) (if (< (string-length input) 3) (string-append output input) (let ((triple (substring/read-only input index (+ index 3)))) (if (string= triple "%20") (old-url-encoding (string-drop input 3) 0 (string-append output "+")) (old-url-encoding (string-drop input 1) 0 (string-append output (string (string-ref input index)))))))) (define r (let ((query q2)) (http-post post-url #:body (string-append "query=" (old-url-encoding (uri-encode query))) #:streaming? #t #:headers `((user-agent . "GNU Guile") (content-type . (application/x-www-form-urlencoded)) (accept . ((,(string->symbol type)))))))) (define (read port) (let ((line (read-line port))) (if (eof-object? line) #t (begin (format #t "Line: ~a~%" line) ;; Tail-recurse until we have processed each line. (read port))))) (read (response-port r))