Prolog Program and External Web API calls

I’m using: SWI-Prolog version 7.6.4

I want code to: Post a JSON payload to an external web API from a Prolog predicate

Can anyone point me to a good working example of this, please?

Have a look at Prolog docs - on that linked page, there’s also information on JSON handling.

Here’s an example below:

:- module(jsondemo, [run/0]).

:- use_module(library(http/http_client), [http_post/4]).
:- use_module(library(http/json), [atom_json_term/3]).


run :-
    atom_json_term(Atom, _{foo: 1, bar: "baz"}, []),
    http_post('https://httpbin.org/post',
              atom(Atom),
              Reply,
              []),
    format("Reply: ~w", [Reply]).
3 Likes

Hmmm. Might be time to extend http_post_data/3 to accept a dict and emit JSON …

3 Likes

Here is a marginally more complete example than the one by @jamesnvc. The idea itself is the same.

The relevant code is at the bottom.