# Web query help needed

**URL:** <https://swi-prolog.discourse.group/t/web-query-help-needed/5587>\
**Category:** Predicate\
**Tags:** how-to\
**Created:** [July 7, 2022, 4:46pm UTC](https://swi-prolog.discourse.group/t/web-query-help-needed/5587 "2022-07-07T16:46:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alan\_in\_Tempe](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@Alan\_in\_Tempe](https://swi-prolog.discourse.group/u/Alan_in_Tempe)\
**Post date:** [July 7, 2022, 4:46pm UTC](https://swi-prolog.discourse.group/t/web-query-help-needed/5587/1 "2022-07-07T16:46:19Z")

</div>

I have access to a huge on-line database that I want to query. I do know how to set up the queries as a web browser URL, and I know how to parse the replies. Every query will be in the general form:

`https://www.zzz.org/index.php?option=com_api&app=zzz&clientid=zzz&key=zzz&resource=zzz&...`

And the reply from to these replies (raw source) on a web browser has the general form:

`{"result":{"id":"15","location_id":"10","location": … }`

I have no concerns about generating the URLs, or analyzing the replies in Prolog. However, I do not know how to use SWI-Prolog to set up my computer and to send and receive web data.

I am new to SWI-Prolog, but have it running great in a terminal window on my Mac. This is an awesome Prolog system! No concerns getting up to speed with it and its debugger. I have 20 years professional Prolog experience prior to my retirement 8 years ago, and now want to work on a personal project that requires me doing these very simple web queries, but I have no prior web programming experience. All help will, of course, be tremendously appreciated!

– Alan Newman

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [July 7, 2022, 5:30pm UTC](https://swi-prolog.discourse.group/t/web-query-help-needed/5587/2 "2022-07-07T17:30:35Z")

</div>

If you want to know how to handle JSON, here’s the [documentation](https://www.swi-prolog.org/pldoc/man?section=jsonsupport).

For communicating between a JavaScript client and a Prolog HTTP server, using JSON, here’s some “tutorial” code: [GitHub - kamahen/swipl-server-js-client: Sample SWI-Prolog server with JavaScript client](https://github.com/kamahen/swipl-server-js-client) (elsewhere in this discussion group there are pointers to Prolog making HTTP queries; there is also an ODBC interface).

---

<div class="post-metadata">

**Author:** ![Alan\_in\_Tempe](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@Alan\_in\_Tempe](https://swi-prolog.discourse.group/u/Alan_in_Tempe)\
**Post date:** [July 7, 2022, 11:18pm UTC](https://swi-prolog.discourse.group/t/web-query-help-needed/5587/3 "2022-07-07T23:18:25Z")

</div>

Thank you, Peter! The kamahen/swipl-server-js-client tutorial looks very appropriate.

Thanks also for the JSON reference. It looks like it will be very useful. Learning JSON with SWI support should be reasonably straightforward, as I have already been able to run my own examples.

However, I am not using Linux, so the getting started instructions from kamahen are not clear to me. I’m guessing that many of the installation steps are already handled in the Mac install package, but I am not sure which steps I can skip. Do I need to install software-properties-common, or add repository (?) spa:swi-prolog/devel, and if so, how do I do that with a Mac?

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [July 7, 2022, 11:43pm UTC](https://swi-prolog.discourse.group/t/web-query-help-needed/5587/4 "2022-07-07T23:43:02Z")

</div>

Linux (Debian, Ubuntu) uses the _Advanced Package Tool_ or _APT_ to install packages; it has a command line interface that is invoked with `sudo apt install ...`  
The `sudo apt-add-repository` is used to make a repository (“PPA”) available.

I don’t know how to install packages on the Mac, but this page might give you some hints (assuming you use `Macports` or `Homebrew`):  
[https://www.swi-prolog.org/build/macos.html](https://www.swi-prolog.org/build/macos.html)

---

<div class="post-metadata">

**Author:** ![Alan\_in\_Tempe](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@Alan\_in\_Tempe](https://swi-prolog.discourse.group/u/Alan_in_Tempe)\
**Post date:** [July 8, 2022, 4:02pm UTC](https://swi-prolog.discourse.group/t/web-query-help-needed/5587/5 "2022-07-08T16:02:21Z")

</div>

Thanks again, Peter. I have had good initial success. The default Mac simple install seems to work fine with my very rudimentary HTTP client program. I loaded the library modules http/http\_client, and http\_open, and was able to invoke `http_get('https://www. ... ', X, [])` to instantiate X to the exact external server reply I expected, `X = '{"result":{"id":"15", ... '`.

My next step is to use other JSON library predicates to simplify the generation and parsing of the HTTP queries and replies before doing my analysis. This has been much easier than I expected, significantly due to your help.

If my program gets the data I need and the research shows anything useful, I will post a link to my results here, in case there is interest. The topic is worldwide gun violence as related to gun density (but with neither a pro- or anti-gun intent). If I get it published, be assured that both the collection of the source data at [www.gunpolicy.org](http://www.gunpolicy.org), and SWI-Prolog will get exemplary credits.

---

<div class="post-metadata">

**Author:** ![JCR](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@JCR](https://swi-prolog.discourse.group/u/JCR)\
**Post date:** [July 8, 2022, 5:37pm UTC](https://swi-prolog.discourse.group/t/web-query-help-needed/5587/6 "2022-07-08T17:37:42Z")

</div>

Hi Alan 🙂  
Another source that you might find useful:

> [@Yet another web applications tutorial](https://swi-prolog.discourse.group/t/yet-another-web-applications-tutorial/566):
>
> While teaching myself how to develop a web application with SWI-Prolog, I’ve taken notes for future reference, which others may find useful since I’ve had to figure much of this out through trial and error. My tutorial differs from Anne Ogborn’s at [http://www.pathwayslms.com/swipltuts/html/index.html](http://www.pathwayslms.com/swipltuts/html/index.html) in that I’ve not used the html\_write library (I’m ideologically opposed to templating), and have gone in directions I personally found initially difficult to grasp from the documentation (though wh…

Hope it helps!  
/JCR

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [July 8, 2022, 6:37pm UTC](https://swi-prolog.discourse.group/t/web-query-help-needed/5587/7 "2022-07-08T18:37:20Z")

</div>

Anne’s tutorial is good, but it’s a bit out of date (there have been some improvements to the http library since she wrote the tutorial). That’s why I wrote my tutorial code, which was extracted from a more complex application that I wrote, initially using Anne’s tutorial.

---

<div class="post-metadata">

**Author:** ![Alan\_in\_Tempe](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@Alan\_in\_Tempe](https://swi-prolog.discourse.group/u/Alan_in_Tempe)\
**Post date:** [July 8, 2022, 6:45pm UTC](https://swi-prolog.discourse.group/t/web-query-help-needed/5587/8 "2022-07-08T18:45:29Z")

</div>

I hadn’t linked the tutorial at kamahen with the author peter.ludemann. Where is that {embarrassed} emoji when you need it?

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [July 8, 2022, 7:01pm UTC](https://swi-prolog.discourse.group/t/web-query-help-needed/5587/9 "2022-07-08T19:01:51Z")

</div>

No worries – that’s what “[kamahen](https://www.jlect.com/entry/1112/kamahen/)” means, more or less 😉  
One of the first words I learned when I lived in Ōsaka, many years ago.

---

<div class="post-metadata">

**Author:** ![Alan\_in\_Tempe](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@Alan\_in\_Tempe](https://swi-prolog.discourse.group/u/Alan_in_Tempe)\
**Post date:** [July 20, 2022, 3:40pm UTC](https://swi-prolog.discourse.group/t/web-query-help-needed/5587/10 "2022-07-20T15:40:25Z")

</div>

I finished my studies on gun violence. The hypothesis was gun violence is strongly correlated to gun density (gun deaths/100K related to number of guns/100, for all developed nations), as was presented on a graph I was given. I wanted to prove the correlation existed, but the analysis failed to demonstrate a significant correlation. On the other hand, the exercise did provide me with a great opportunity to enjoy some Prolog coding, learn SWI-Prolog, and learn how to do API web interfacing.

Since the hypothesis was not supported, I will not be doing a full writeup for publication, but I did do a quick summary of the goal, procedures, results, supporting data, and the Prolog code. I will email that write-up to anyone interested (just provide me your email address).

Here are the required libraries and the key predicate clause for accessing the web data:

```prolog
:- use_module(library(http/http_open)) .
:- use_module(library(http/http_client)) . 
:- use_module(library(http/json)) .
:- use_module(library(http/json_convert)) .

ask_gunpolicy(Topic, Location, Country, Reply) :- 
   credentials(User, Key) ,
   atomic_list_concat(
      [ 'https://www .gunpolicy .org/index.php?option=com_api',
        '&app=gpodatapage',
        '&clientid=',User,
        '&key=', Key,
        '&format=raw&resource=getcategorydata',
        '&category=', Topic, 
        '&location_id=', Location
      ], Query) ,
   http_get(Query, Reply, []) , 
   atom_json_term(Reply, json([result=json(JT)]), []) ,
    {rest omitted}

```

This predicates takes three inputs, and writes one line of formatted data to output per invocation. Data is returned as a fourth argument mostly to handle exceptions from incorrectly formatted web responses.

Additional code generates the numerous ask\_gunpolicy/4 calls for each of 50 countries, and 8 different data categories per country. The final output is comma separated data (“Country, Category1, …, Category 8,”) that is imported directly into Excel for statistical analysis and graphing. Also, each returned Category data has multiple elements that include source reference, and year/value data pairs, and the Prolog separates all the values and returns an average value. Had the study shown a significant correlation, then data would have been further broken down by year, and more of the stat analysis would have been done in Prolog (rather than just a simple averaging).’

Thanks for the much appreciated help and guidance!
