Here is an example/demonstration of using SWI-Prolog on Windows to create a local HTTP web server that can receive and send JSON. To test the URL, curl is used.
While many curl commands are demonstrated for use on Linux, suttle syntax changes are needed with Windows.
One of the simplest examples of this can be found here in the “Web services in Prolog” section.
https://www.monolune.com/articles/web-programming-in-swi-prolog/
Prolog file: server.pl
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_json)).
% URL handlers.
:- http_handler('/', handle_request, []).
% Calculates a + b.
solve(_{a:X, b:Y}, _{answer:N}) :-
number(X),
number(Y),
N is X + Y.
handle_request(Request) :-
http_read_json_dict(Request, Query),
solve(Query, Solution),
reply_json_dict(Solution).
server(Port) :-
http_server(http_dispatch, [port(Port)]).
:- initialization(server(8000)).
Example run
Using SWI-Prolog toplevel start the HTTP server.
Welcome to SWI-Prolog (threaded, 64 bits, version 9.1.8)
...
?- working_directory(_,'C:/Users/Groot').
true.
?- [server].
% Started server at http://localhost:8000/
true.
Using Windows command prompt run a Curl command.
C:\Users\Groot>curl --header "Content-Type: application/json" --request POST --data "{\"a\": 1, \"b\": 2 }" "http://localhost:8000"
{"answer":3}
Notice the difference in the syntax for the delimiters.
Given with blog entry and results in error.
curl --header 'Content-Type: application/json' --request POST --data '{"a": 1, "b": 2 }' 'http://localhost:8000'
Modified version that works.
curl --header "Content-Type: application/json" --request POST --data "{\"a\": 1, \"b\": 2 }" "http://localhost:8000"
The error message using the curl command as given
C:\Users\Groot>curl --header 'Content-Type: application/json' --request POST --data '{"a": 1, "b": 2 }' 'http://localhost:8000'
curl: (6) Could not resolve host: application
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (7) Failed to connect to 0.0.0.2 port 80 after 0 ms: Couldn't connect to server
curl: (3) unmatched close brace/bracket in URL position 1:
}'
^
After much searching, ChatGPT gave a correct remedy to modify the delimiter syntax when given an error message.
Prompt
ChatGPT May 12 Version
Please explain these curl command error(s)
C:\Users\Groot>curl --header 'Content-Type: application/json' --request POST --data '{"a": 1, "b": 2 }' 'http://localhost:8000'
curl: (6) Could not resolve host: application
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (7) Failed to connect to 0.0.0.2 port 80 after 0 ms: Couldn't connect to server
curl: (3) unmatched close brace/bracket in URL position 1:
}'
Completion
The error messages you’re seeing are due to incorrect syntax in your curl command. In Windows command prompt, you should use double quotes ("
) instead of single quotes ('
) for enclosing parameters.
Here’s the corrected command:
curl --header "Content-Type: application/json" --request POST --data "{\"a\": 1, \"b\": 2}" "http://localhost:8000"
In this command:
-
--header "Content-Type: application/json"
sets theContent-Type
header toapplication/json
, indicating that the request body contains JSON data. -
--request POST
specifies that the HTTP method should be POST. -
--data "{\"a\": 1, \"b\": 2}"
sets the request body to a JSON object with propertiesa
andb
. -
"http://localhost:8000"
is the URL to which the request is sent.
The JSON data in the --data
parameter is enclosed in escaped double quotes (\"
) because the entire parameter is already enclosed in double quotes. This is necessary because Windows command prompt does not support single quotes for enclosing strings.