I inherited some server code I don't fully understand and have been tasked with having it return JSON instead of plain strings. Here's what the current code looks like:
:- http_handler('/', web_form, ).
:- http_handler('/dataexchange', dataexchange_handler, [time_limit(infinite)]).
member(method(post), Request), !,
http_read_data(Request, DataString, ),
general_predicate( DataString, OutString ),
format('Content-type: text/html~n~n', ),
general_predicate( InString, Out ) :-
term_string( TotalTerm, InString ),
call_process_request( TotalTerm, ResultTerm ),
term_string( ResultTerm, Out ).
The 'portray_clause' seems a bit kooky, but the current functionality definitely works (returning strings). I can construct SWIPL json objects using ResultTerm, but am not entirely sure what to do with them subsequently.