質問
How do I make an HTTP request in SWI Prolog?
回答
:- use_module(library(http/http_client)).
make_request(URL) :-
http_get(URL, Reply, []),
format('~s~n', [Reply]).
動くプログラムに改造
http_get.pl
/* ------------------------------------------------------------------- */
/*
http_get.pl
Apr/02/2023
*/
/* ------------------------------------------------------------------- */
:- initialization main.
:- use_module(library(http/http_client)).
make_request(URL) :-
http_get(URL, Reply, []),
format('~s~n', [Reply]).
/* ------------------------------------------------------------------- */
main() :-
format('*** 開始 ***\n'),
URL = 'https://httpbin.org/get',
make_request(URL),
format('*** 終了 ***\n'),
halt.
/* ------------------------------------------------------------------- */
実行結果
$ swipl -f http_get.pl
*** 開始 ***
{
"args": {},
"headers": {
"Host": "httpbin.org",
"User-Agent": "SWI-Prolog",
"X-Amzn-Trace-Id": "Root=1-6428de5e-6f6f25b85e7377b1365f1dc5"
},
"origin": "219.126.142.122",
"url": "https://httpbin.org/get"
}
*** 終了 ***
課題
スニペットでなく、実際に動くプログラムを提示してもらうにはどうしたらよいか。
参考
私が4年半まえに書いた同じプログラム
SWI-Prolog の http クライアントの使い方 (Get)