LoginSignup
0
0

More than 1 year has passed since last update.

ChatGPT で SWI Prolog のプログラムを作成

Posted at

質問

How do I make an HTTP request in SWI Prolog?

回答

image.png

:- 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)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0