How to redirect output of rdf_save_turtle to http output in SWI Prolog?

I’m using SWI-Prolog, both semweb and HTTP libraries at the same time. Now I want to return a turtle file in an API, which seems to be possible because rdf_save_turtle saves to a stream and http_reply should be able to return from a stream.

However, I’m not able to make it work.

:- http_handler(root('_api/query'), query, [method(get)]).

query(Request) :-
    stream_property(Response, write),
    format('Content-Type: text/turtle~n~n'),
    rdf_save_turtle(Response, []).

According to the docs, in the query term, there’s a default output stream (used by format) which is the HTTP output. However, I’m not able to get it with stream_property so I can’t pass to rdf_save_turtle a valid output stream.

How could you get the default output stream?


This is double posted at StackOverflow.

In SWI-Prolog, current_output is a stream alias for the default output stream. Surely stream_property(Response, write) is not going to work as it will pick some arbitrary stream opened for writing. There is also the current_output/1 that is available in several Prolog systems. It also exists in SWI-Prolog, but given current_output you hardly ever need it.