# Prolog Program and External Web API calls

**URL:** https://swi-prolog.discourse.group/t/prolog-program-and-external-web-api-calls/1264
**Category:** Help!
**Created:** [September 15, 2019, 9:30pm UTC](https://swi-prolog.discourse.group/t/prolog-program-and-external-web-api-calls/1264 "2019-09-15T21:30:49Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ppowell](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ppowell/32/564_2.png) [@ppowell](https://swi-prolog.discourse.group/u/ppowell)
#### Post date: [September 15, 2019, 9:30pm UTC](https://swi-prolog.discourse.group/t/prolog-program-and-external-web-api-calls/1264/1 "2019-09-15T21:30:49Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![jamesnvc](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jamesnvc/32/14_2.png) [@jamesnvc](https://swi-prolog.discourse.group/u/jamesnvc)
#### Post date: [September 15, 2019, 10:39pm UTC](https://swi-prolog.discourse.group/t/prolog-program-and-external-web-api-calls/1264/2 "2019-09-15T22:39:14Z")

</div>

Have a look at [Prolog docs](https://www.swi-prolog.org/pldoc/man?section=http-clients) - on that linked page, there’s also information on JSON handling.

Here’s an example below:

```prolog
:- 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]).

```

---

<div class="post-metadata">

### Author: ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)
#### Post date: [September 16, 2019, 7:13am UTC](https://swi-prolog.discourse.group/t/prolog-program-and-external-web-api-calls/1264/3 "2019-09-16T07:13:19Z")

</div>

Hmmm. Might be time to extend http\_post\_data/3 to accept a dict and emit JSON …

---

<div class="post-metadata">

### Author: ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)
#### Post date: [September 16, 2019, 7:46am UTC](https://swi-prolog.discourse.group/t/prolog-program-and-external-web-api-calls/1264/4 "2019-09-16T07:46:32Z")

</div>

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

> <https://github.com/borisvassilev/vindinium-swi/blob/1132d9d92f914a87653387807b2378274fced1a1/vindinium.pl>

The relevant code is at the bottom.
