
http_json.pl -- HTTP JSON Plugin moduleThis module adds hooks to several parts of the HTTP libraries, making them JSON-aware. Notably:
application/json and
application/jsonrequest content to a JSON term.post(json(Term)) to issue a POST
request with JSON content.Accept header prefers application/json over
text/html.Typically JSON is used by Prolog HTTP servers. This module supports two JSON representations: the classical representation and the new representation supported by the SWI-Prolog version 7 extended data types. Below is a skeleton for handling a JSON request, answering in JSON using the classical interface.
handle(Request) :-
http_read_json(Request, JSONIn),
json_to_prolog(JSONIn, PrologIn),
<compute>(PrologIn, PrologOut), % application body
prolog_to_json(PrologOut, JSONOut),
reply_json(JSONOut).
When using dicts, the conversion step is generally not needed and the code becomes:
handle(Request) :-
http_read_json_dict(Request, DictIn),
<compute>(DictIn, DictOut),
reply_json(DictOut).
This module also integrates JSON support into the http client provided by http_client.pl. Posting a JSON query and processing the JSON reply (or any other reply understood by http_read_data/3) is as simple as below, where Term is a JSON term as described in json.pl and reply is of the same format if the server replies with JSON.
...,
http_post(URL, json(Term), Reply, [])
http_client:http_convert_data(+In, +Fields, -Data, +Options)[multifile]term or dict. If the value is dict,
json_read_dict/3 is used.
is_json_content_type(+ContentType) is semidet
json_type(?MediaType) is semidet[multifile]
http:post_data_hook(+Data, +Out:stream, +HdrExtra) is semidet[multifile]http_post(URL, json(Term), Reply, Options) http_post(URL, json(Term, Options), Reply, Options)
If Options are passed, these are handed to json_write/3. In addition, this option is processed:
dict, json_write_dict/3 is used to write the
output. This is default if json(Dict) is passed.
http_read_json(+Request, -JSON) is det
http_read_json(+Request, -JSON, +Options) is detterm (default) to generate a classical Prolog
term or dict to exploit the SWI-Prolog version 7 data type
extensions. See json_read_dict/3.
http_read_json_dict(+Request, -Dict) is det
http_read_json_dict(+Request, -Dict, +Options) is det
reply_json(+JSONTerm) is det
reply_json(+JSONTerm, +Options) is detContent-type is application/json;
charset=UTF8. charset=UTF8 should not be required
because JSON is defined to be UTF-8 encoded, but some
clients insist on it.term (classical json representation) or dict
to use the new dict representation. If omitted and Term
is a dict, dict is assumed. SWI-Prolog Version 7.
reply_json_dict(+JSONTerm) is det
reply_json_dict(+JSONTerm, +Options) is det
http_read_json(+Request, -JSON) is det
http_read_json(+Request, -JSON, +Options) is detterm (default) to generate a classical Prolog
term or dict to exploit the SWI-Prolog version 7 data type
extensions. See json_read_dict/3.
http_read_json_dict(+Request, -Dict) is det
http_read_json_dict(+Request, -Dict, +Options) is det
reply_json(+JSONTerm) is det
reply_json(+JSONTerm, +Options) is detContent-type is application/json;
charset=UTF8. charset=UTF8 should not be required
because JSON is defined to be UTF-8 encoded, but some
clients insist on it.term (classical json representation) or dict
to use the new dict representation. If omitted and Term
is a dict, dict is assumed. SWI-Prolog Version 7.
reply_json_dict(+JSONTerm) is det
reply_json_dict(+JSONTerm, +Options) is det