用 JSON 回复 HTTP 请求
Replying with JSON to an HTTP request
我尝试在使用此请求后发送 JSON:http://localhost:8080/json_test?id=1
到目前为止,这是我的代码:
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_parameters)).
:- http_handler('/json_test', json_test, []).
nomeEdificio(1, "Torre dos Clérigos").
nomeEdificio(2, "Fonte dos Leões").
anoConstrucao(1, 1754).
anoConstrucao(2, 1882).
anoInauguracao(1, 1763).
anoInauguracao(2, 1884).
server(Port) :-
http_server(http_dispatch, [port(Port)]).
json_test(Request) :-
http_parameters(Request,
[ id(Id, [])]
),
compute_request(Id,PrologOut),
prolog_to_json(PrologOut,JSonOut),
reply_json(JSonOut).
compute_request(Id,PrologOut):-
nomeEdificio(Id,N), anoConstrucao(Id,Ac), anoInauguracao(Id,Ai), % Some consulting operations
PrologOut = json([ name=N,
dates=json([yearConstruction=Ac, yearInauguration=Ai])
]).
但是我得到这个错误:
"Internal server error
goal unexpectedly failed: user:json_test([protocol(http),peer(ip(127,0,0,1)),pool(client('httpd@8080',user:http_dispatch,<stream>(00000000056CE8C0),<stream>(00000000056CCD90))),input(<stream>(00000000056CE8C0)),method(get),request_uri('/json_test?id=1'),path('/json_test'),search([id='1']),http_version(1-1),host(localhost),port(8080),user_agent('Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0'),accept([media(text/html,[],1.0,[]),media(application/'xhtml+xml',[],1.0,[]),media(application/xml,[],0.9,[]),media(_G3007/_G3008,[],0.8,[])]),accept_language('en-US,en;q=0.5'),accept_encoding('gzip, deflate'),connection('keep-alive'),upgrade_insecure_requests('1')])"
我不知道哪里出了问题,关于如何使用这些 Prolog 谓词的文档不多。欢迎任何帮助,谢谢。
示例不完整
当我加载您 post 编辑的代码时,我得到:
catch/3: Undefined procedure: http_handler/3
所以,我补充说:
:- use_module(library(http/thread_httpd)).
调度
接下来,当我加载修改后的示例并使用 ?- server(4040).
启动服务器,然后访问 http://localhost:4040/json_test
,我得到:
Internal server error
Undefined procedure: http_dispatch/1
所以,我补充说:
:- use_module(library(http/http_dispatch)).
参数
然后,当我重新加载并重新访问时,我得到:
Internal server error
Undefined procedure: http_parameters/2
所以,我补充说:
:- use_module(library(http/http_parameters)).
和id
当然要提供一个id
,所以我访问例如:
http://localhost:4040/json_test?id=x
然后,我得到:
Internal server error
Undefined procedure: nomeEdificio/2
要调试此类问题,需要尽可能简化一切,post简化,自包含 带有明确说明如何 运行 您的测试用例的示例。
上面你省略了:
- 你显然已经包含在你的程序中的基本指令
- 有关如何运行您的服务器的基本信息
- 程序中使用的基本谓词的定义。
你不能指望别人来填补如此极端的遗漏。
正在回复JSON
至于您的实际问题,您的主要问题完全被您post编辑的内容所掩盖。要回复JSON,这里有一个例子。
首先,获得JSON:
:- use_module(library(http/json_convert)).
:- json_object
point(x:integer, y:integer).
示例查询:
?- prolog_to_json(point(10,30), JSON).
JSON = json([x=10, y=30]).
这个需要上面的指令,否则不起作用!这是 直接取自 SWI-Prolog 文档,这是了解这些主题的更多信息的好资源。
那么,回复这样一个词:
?- prolog_to_json(point(10,30), JSON),
reply_json(JSON).
Content-type: application/json; charset=UTF-8
{"x":10, "y":30}
...
因此,从小处着手:一旦您拥有 this 运行ning without HTTP 服务器,您可以插入 相同的代码到实际调度中。
我尝试在使用此请求后发送 JSON:http://localhost:8080/json_test?id=1 到目前为止,这是我的代码:
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_parameters)).
:- http_handler('/json_test', json_test, []).
nomeEdificio(1, "Torre dos Clérigos").
nomeEdificio(2, "Fonte dos Leões").
anoConstrucao(1, 1754).
anoConstrucao(2, 1882).
anoInauguracao(1, 1763).
anoInauguracao(2, 1884).
server(Port) :-
http_server(http_dispatch, [port(Port)]).
json_test(Request) :-
http_parameters(Request,
[ id(Id, [])]
),
compute_request(Id,PrologOut),
prolog_to_json(PrologOut,JSonOut),
reply_json(JSonOut).
compute_request(Id,PrologOut):-
nomeEdificio(Id,N), anoConstrucao(Id,Ac), anoInauguracao(Id,Ai), % Some consulting operations
PrologOut = json([ name=N,
dates=json([yearConstruction=Ac, yearInauguration=Ai])
]).
但是我得到这个错误:
"Internal server error
goal unexpectedly failed: user:json_test([protocol(http),peer(ip(127,0,0,1)),pool(client('httpd@8080',user:http_dispatch,<stream>(00000000056CE8C0),<stream>(00000000056CCD90))),input(<stream>(00000000056CE8C0)),method(get),request_uri('/json_test?id=1'),path('/json_test'),search([id='1']),http_version(1-1),host(localhost),port(8080),user_agent('Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0'),accept([media(text/html,[],1.0,[]),media(application/'xhtml+xml',[],1.0,[]),media(application/xml,[],0.9,[]),media(_G3007/_G3008,[],0.8,[])]),accept_language('en-US,en;q=0.5'),accept_encoding('gzip, deflate'),connection('keep-alive'),upgrade_insecure_requests('1')])"
我不知道哪里出了问题,关于如何使用这些 Prolog 谓词的文档不多。欢迎任何帮助,谢谢。
示例不完整
当我加载您 post 编辑的代码时,我得到:
catch/3: Undefined procedure: http_handler/3
所以,我补充说:
:- use_module(library(http/thread_httpd)).
调度
接下来,当我加载修改后的示例并使用 ?- server(4040).
启动服务器,然后访问 http://localhost:4040/json_test
,我得到:
Internal server error Undefined procedure: http_dispatch/1
所以,我补充说:
:- use_module(library(http/http_dispatch)).
参数
然后,当我重新加载并重新访问时,我得到:
Internal server error Undefined procedure: http_parameters/2
所以,我补充说:
:- use_module(library(http/http_parameters)).
和id
当然要提供一个id
,所以我访问例如:
http://localhost:4040/json_test?id=x
然后,我得到:
Internal server error Undefined procedure: nomeEdificio/2
要调试此类问题,需要尽可能简化一切,post简化,自包含 带有明确说明如何 运行 您的测试用例的示例。
上面你省略了:
- 你显然已经包含在你的程序中的基本指令
- 有关如何运行您的服务器的基本信息
- 程序中使用的基本谓词的定义。
你不能指望别人来填补如此极端的遗漏。
正在回复JSON
至于您的实际问题,您的主要问题完全被您post编辑的内容所掩盖。要回复JSON,这里有一个例子。
首先,获得JSON:
:- use_module(library(http/json_convert)). :- json_object point(x:integer, y:integer).
示例查询:
?- prolog_to_json(point(10,30), JSON). JSON = json([x=10, y=30]).
这个需要上面的指令,否则不起作用!这是 直接取自 SWI-Prolog 文档,这是了解这些主题的更多信息的好资源。
那么,回复这样一个词:
?- prolog_to_json(point(10,30), JSON), reply_json(JSON). Content-type: application/json; charset=UTF-8 {"x":10, "y":30} ...
因此,从小处着手:一旦您拥有 this 运行ning without HTTP 服务器,您可以插入 相同的代码到实际调度中。