Prolog receiving Json post
This is my first question in stackoverflow so, bear with me.
I'm tring to build a simple Prolog api that receives json posts and after processing them, sends another json post back. I found this code to receive the json:
handle(Request) :-
http_read_json_dict(Request, DictIn),
compute(DictIn, DictOut),
reply_json(DictOut).
I assume that compute
is a custom predicate, which for test purposes is test(D,D)
.
The problem is that when I try to test handle(Request)
in swi-prolog I get either the error message ERROR: atom_codes/2: Arguments are not sufficiently instantiated
or I get false.
I assume I just have to pass a json in the Request
but it's not working. I also tried sending a post with Postman with a json file in the body (raw and application/json) but i get a timeout, which eh..yea...
My question is what do I write in Request so that it instantiates it properly?
Thanks in advance and sorry if it's a bad/noobie question, but swi-prolog documentation is terrible and i can't find an answer anywhere.
json http post prolog swi-prolog
add a comment |
This is my first question in stackoverflow so, bear with me.
I'm tring to build a simple Prolog api that receives json posts and after processing them, sends another json post back. I found this code to receive the json:
handle(Request) :-
http_read_json_dict(Request, DictIn),
compute(DictIn, DictOut),
reply_json(DictOut).
I assume that compute
is a custom predicate, which for test purposes is test(D,D)
.
The problem is that when I try to test handle(Request)
in swi-prolog I get either the error message ERROR: atom_codes/2: Arguments are not sufficiently instantiated
or I get false.
I assume I just have to pass a json in the Request
but it's not working. I also tried sending a post with Postman with a json file in the body (raw and application/json) but i get a timeout, which eh..yea...
My question is what do I write in Request so that it instantiates it properly?
Thanks in advance and sorry if it's a bad/noobie question, but swi-prolog documentation is terrible and i can't find an answer anywhere.
json http post prolog swi-prolog
I find swi-prolog documentation very good! Maybe you should read Anne Ogborn's excellent tutorial!
– gniourf_gniourf
Dec 12 '15 at 17:26
Thanks for the tip. I tried reading that tutorial but I coudnl't find any information regarding json requests and replies...
– Rui Vaz
Dec 12 '15 at 21:15
add a comment |
This is my first question in stackoverflow so, bear with me.
I'm tring to build a simple Prolog api that receives json posts and after processing them, sends another json post back. I found this code to receive the json:
handle(Request) :-
http_read_json_dict(Request, DictIn),
compute(DictIn, DictOut),
reply_json(DictOut).
I assume that compute
is a custom predicate, which for test purposes is test(D,D)
.
The problem is that when I try to test handle(Request)
in swi-prolog I get either the error message ERROR: atom_codes/2: Arguments are not sufficiently instantiated
or I get false.
I assume I just have to pass a json in the Request
but it's not working. I also tried sending a post with Postman with a json file in the body (raw and application/json) but i get a timeout, which eh..yea...
My question is what do I write in Request so that it instantiates it properly?
Thanks in advance and sorry if it's a bad/noobie question, but swi-prolog documentation is terrible and i can't find an answer anywhere.
json http post prolog swi-prolog
This is my first question in stackoverflow so, bear with me.
I'm tring to build a simple Prolog api that receives json posts and after processing them, sends another json post back. I found this code to receive the json:
handle(Request) :-
http_read_json_dict(Request, DictIn),
compute(DictIn, DictOut),
reply_json(DictOut).
I assume that compute
is a custom predicate, which for test purposes is test(D,D)
.
The problem is that when I try to test handle(Request)
in swi-prolog I get either the error message ERROR: atom_codes/2: Arguments are not sufficiently instantiated
or I get false.
I assume I just have to pass a json in the Request
but it's not working. I also tried sending a post with Postman with a json file in the body (raw and application/json) but i get a timeout, which eh..yea...
My question is what do I write in Request so that it instantiates it properly?
Thanks in advance and sorry if it's a bad/noobie question, but swi-prolog documentation is terrible and i can't find an answer anywhere.
json http post prolog swi-prolog
json http post prolog swi-prolog
edited Nov 22 '18 at 17:06
j4n bur53
6,16732261
6,16732261
asked Dec 12 '15 at 16:56
Rui VazRui Vaz
266
266
I find swi-prolog documentation very good! Maybe you should read Anne Ogborn's excellent tutorial!
– gniourf_gniourf
Dec 12 '15 at 17:26
Thanks for the tip. I tried reading that tutorial but I coudnl't find any information regarding json requests and replies...
– Rui Vaz
Dec 12 '15 at 21:15
add a comment |
I find swi-prolog documentation very good! Maybe you should read Anne Ogborn's excellent tutorial!
– gniourf_gniourf
Dec 12 '15 at 17:26
Thanks for the tip. I tried reading that tutorial but I coudnl't find any information regarding json requests and replies...
– Rui Vaz
Dec 12 '15 at 21:15
I find swi-prolog documentation very good! Maybe you should read Anne Ogborn's excellent tutorial!
– gniourf_gniourf
Dec 12 '15 at 17:26
I find swi-prolog documentation very good! Maybe you should read Anne Ogborn's excellent tutorial!
– gniourf_gniourf
Dec 12 '15 at 17:26
Thanks for the tip. I tried reading that tutorial but I coudnl't find any information regarding json requests and replies...
– Rui Vaz
Dec 12 '15 at 21:15
Thanks for the tip. I tried reading that tutorial but I coudnl't find any information regarding json requests and replies...
– Rui Vaz
Dec 12 '15 at 21:15
add a comment |
2 Answers
2
active
oldest
votes
I'm not too sure you fully understand how Prolog and swi-prolog's web framework works.
Here's a step-by-step mini-tutorial to get you started:
copy this in a file called
myserver.pl
:
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_json)).
:- http_handler(root(.),handle,).
server(Port) :-
http_server(http_dispatch,[port(Port)]).
handle(Request) :-
format(user_output,"I'm here~n",),
http_read_json(Request, DictIn,[json_object(term)]),
format(user_output,"Request is: ~p~n",[Request]),
format(user_output,"DictIn is: ~p~n",[DictIn]),
DictOut=DictIn,
reply_json(DictOut).
launch swi-prolog and in the main repl type:
[myserver].
to consult your file. You should have no errors. Then launch your server, say on port 8000:
server(8000).
You should have the following reply:
% Started server at http://localhost:8000/
open another terminal and post some json using
curl
:
curl -H "Content-Type: application/json" -X POST -d '{"hello":"world"}' http://localhost:8000
you should have the following reply:
{"hello":"world"}
and in the running prolog you should see these messages:
I'm here
Request is: [protocol(http),peer(ip(127,0,0,1)),pool(client('httpd@8000',user:http_dispatch,<stream>(0x7facc4026b20),<stream>(0x7facc4027040))),input(<stream>(0x7facc4026b20)),method(post),request_uri(/),path(/),http_version(1-1),user_agent('curl/7.35.0'),host(localhost),port(8000),accept([media(_G841/_G842,,1.0,)]),content_type('application/json'),content_length(17)]
DictIn is: json([hello=world])
If you do any modifications to the file myserver.pl
, you just need to type make.
in prolog's repl.
I can't recommend enough Anne Ogborn's excellent tutorial. And by the way, swi-prolog's documentation is very good.
Thank you so much for your time. That is extremely helpful. I'm very much an example driven learner and I find step by step instrucions the best way to understand what stuff does. The problem about swi-prolog documentation is that they only offer the description of each predicate and assume we know how to use them, which I don't. I'm still to find a good place for prolog implementation examples. Even google failed me this time.
– Rui Vaz
Dec 12 '15 at 21:18
add a comment |
There are also a couple of JSON reader/writers around:
Thise modules take atoms and is implemented via DCG:
https://github.com/khueue/prolog-json/tree/master/src
This module is coded directively over ISO streams:
https://gist.github.com/jburse/63986bf525784d6d8cf99db132538d67#file-json_io-p
Both approaches don't require Prolog dicts and are suitable for a wide range of Prolog systems.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34242458%2fprolog-receiving-json-post%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm not too sure you fully understand how Prolog and swi-prolog's web framework works.
Here's a step-by-step mini-tutorial to get you started:
copy this in a file called
myserver.pl
:
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_json)).
:- http_handler(root(.),handle,).
server(Port) :-
http_server(http_dispatch,[port(Port)]).
handle(Request) :-
format(user_output,"I'm here~n",),
http_read_json(Request, DictIn,[json_object(term)]),
format(user_output,"Request is: ~p~n",[Request]),
format(user_output,"DictIn is: ~p~n",[DictIn]),
DictOut=DictIn,
reply_json(DictOut).
launch swi-prolog and in the main repl type:
[myserver].
to consult your file. You should have no errors. Then launch your server, say on port 8000:
server(8000).
You should have the following reply:
% Started server at http://localhost:8000/
open another terminal and post some json using
curl
:
curl -H "Content-Type: application/json" -X POST -d '{"hello":"world"}' http://localhost:8000
you should have the following reply:
{"hello":"world"}
and in the running prolog you should see these messages:
I'm here
Request is: [protocol(http),peer(ip(127,0,0,1)),pool(client('httpd@8000',user:http_dispatch,<stream>(0x7facc4026b20),<stream>(0x7facc4027040))),input(<stream>(0x7facc4026b20)),method(post),request_uri(/),path(/),http_version(1-1),user_agent('curl/7.35.0'),host(localhost),port(8000),accept([media(_G841/_G842,,1.0,)]),content_type('application/json'),content_length(17)]
DictIn is: json([hello=world])
If you do any modifications to the file myserver.pl
, you just need to type make.
in prolog's repl.
I can't recommend enough Anne Ogborn's excellent tutorial. And by the way, swi-prolog's documentation is very good.
Thank you so much for your time. That is extremely helpful. I'm very much an example driven learner and I find step by step instrucions the best way to understand what stuff does. The problem about swi-prolog documentation is that they only offer the description of each predicate and assume we know how to use them, which I don't. I'm still to find a good place for prolog implementation examples. Even google failed me this time.
– Rui Vaz
Dec 12 '15 at 21:18
add a comment |
I'm not too sure you fully understand how Prolog and swi-prolog's web framework works.
Here's a step-by-step mini-tutorial to get you started:
copy this in a file called
myserver.pl
:
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_json)).
:- http_handler(root(.),handle,).
server(Port) :-
http_server(http_dispatch,[port(Port)]).
handle(Request) :-
format(user_output,"I'm here~n",),
http_read_json(Request, DictIn,[json_object(term)]),
format(user_output,"Request is: ~p~n",[Request]),
format(user_output,"DictIn is: ~p~n",[DictIn]),
DictOut=DictIn,
reply_json(DictOut).
launch swi-prolog and in the main repl type:
[myserver].
to consult your file. You should have no errors. Then launch your server, say on port 8000:
server(8000).
You should have the following reply:
% Started server at http://localhost:8000/
open another terminal and post some json using
curl
:
curl -H "Content-Type: application/json" -X POST -d '{"hello":"world"}' http://localhost:8000
you should have the following reply:
{"hello":"world"}
and in the running prolog you should see these messages:
I'm here
Request is: [protocol(http),peer(ip(127,0,0,1)),pool(client('httpd@8000',user:http_dispatch,<stream>(0x7facc4026b20),<stream>(0x7facc4027040))),input(<stream>(0x7facc4026b20)),method(post),request_uri(/),path(/),http_version(1-1),user_agent('curl/7.35.0'),host(localhost),port(8000),accept([media(_G841/_G842,,1.0,)]),content_type('application/json'),content_length(17)]
DictIn is: json([hello=world])
If you do any modifications to the file myserver.pl
, you just need to type make.
in prolog's repl.
I can't recommend enough Anne Ogborn's excellent tutorial. And by the way, swi-prolog's documentation is very good.
Thank you so much for your time. That is extremely helpful. I'm very much an example driven learner and I find step by step instrucions the best way to understand what stuff does. The problem about swi-prolog documentation is that they only offer the description of each predicate and assume we know how to use them, which I don't. I'm still to find a good place for prolog implementation examples. Even google failed me this time.
– Rui Vaz
Dec 12 '15 at 21:18
add a comment |
I'm not too sure you fully understand how Prolog and swi-prolog's web framework works.
Here's a step-by-step mini-tutorial to get you started:
copy this in a file called
myserver.pl
:
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_json)).
:- http_handler(root(.),handle,).
server(Port) :-
http_server(http_dispatch,[port(Port)]).
handle(Request) :-
format(user_output,"I'm here~n",),
http_read_json(Request, DictIn,[json_object(term)]),
format(user_output,"Request is: ~p~n",[Request]),
format(user_output,"DictIn is: ~p~n",[DictIn]),
DictOut=DictIn,
reply_json(DictOut).
launch swi-prolog and in the main repl type:
[myserver].
to consult your file. You should have no errors. Then launch your server, say on port 8000:
server(8000).
You should have the following reply:
% Started server at http://localhost:8000/
open another terminal and post some json using
curl
:
curl -H "Content-Type: application/json" -X POST -d '{"hello":"world"}' http://localhost:8000
you should have the following reply:
{"hello":"world"}
and in the running prolog you should see these messages:
I'm here
Request is: [protocol(http),peer(ip(127,0,0,1)),pool(client('httpd@8000',user:http_dispatch,<stream>(0x7facc4026b20),<stream>(0x7facc4027040))),input(<stream>(0x7facc4026b20)),method(post),request_uri(/),path(/),http_version(1-1),user_agent('curl/7.35.0'),host(localhost),port(8000),accept([media(_G841/_G842,,1.0,)]),content_type('application/json'),content_length(17)]
DictIn is: json([hello=world])
If you do any modifications to the file myserver.pl
, you just need to type make.
in prolog's repl.
I can't recommend enough Anne Ogborn's excellent tutorial. And by the way, swi-prolog's documentation is very good.
I'm not too sure you fully understand how Prolog and swi-prolog's web framework works.
Here's a step-by-step mini-tutorial to get you started:
copy this in a file called
myserver.pl
:
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_json)).
:- http_handler(root(.),handle,).
server(Port) :-
http_server(http_dispatch,[port(Port)]).
handle(Request) :-
format(user_output,"I'm here~n",),
http_read_json(Request, DictIn,[json_object(term)]),
format(user_output,"Request is: ~p~n",[Request]),
format(user_output,"DictIn is: ~p~n",[DictIn]),
DictOut=DictIn,
reply_json(DictOut).
launch swi-prolog and in the main repl type:
[myserver].
to consult your file. You should have no errors. Then launch your server, say on port 8000:
server(8000).
You should have the following reply:
% Started server at http://localhost:8000/
open another terminal and post some json using
curl
:
curl -H "Content-Type: application/json" -X POST -d '{"hello":"world"}' http://localhost:8000
you should have the following reply:
{"hello":"world"}
and in the running prolog you should see these messages:
I'm here
Request is: [protocol(http),peer(ip(127,0,0,1)),pool(client('httpd@8000',user:http_dispatch,<stream>(0x7facc4026b20),<stream>(0x7facc4027040))),input(<stream>(0x7facc4026b20)),method(post),request_uri(/),path(/),http_version(1-1),user_agent('curl/7.35.0'),host(localhost),port(8000),accept([media(_G841/_G842,,1.0,)]),content_type('application/json'),content_length(17)]
DictIn is: json([hello=world])
If you do any modifications to the file myserver.pl
, you just need to type make.
in prolog's repl.
I can't recommend enough Anne Ogborn's excellent tutorial. And by the way, swi-prolog's documentation is very good.
answered Dec 12 '15 at 18:23
gniourf_gniourfgniourf_gniourf
29.9k56483
29.9k56483
Thank you so much for your time. That is extremely helpful. I'm very much an example driven learner and I find step by step instrucions the best way to understand what stuff does. The problem about swi-prolog documentation is that they only offer the description of each predicate and assume we know how to use them, which I don't. I'm still to find a good place for prolog implementation examples. Even google failed me this time.
– Rui Vaz
Dec 12 '15 at 21:18
add a comment |
Thank you so much for your time. That is extremely helpful. I'm very much an example driven learner and I find step by step instrucions the best way to understand what stuff does. The problem about swi-prolog documentation is that they only offer the description of each predicate and assume we know how to use them, which I don't. I'm still to find a good place for prolog implementation examples. Even google failed me this time.
– Rui Vaz
Dec 12 '15 at 21:18
Thank you so much for your time. That is extremely helpful. I'm very much an example driven learner and I find step by step instrucions the best way to understand what stuff does. The problem about swi-prolog documentation is that they only offer the description of each predicate and assume we know how to use them, which I don't. I'm still to find a good place for prolog implementation examples. Even google failed me this time.
– Rui Vaz
Dec 12 '15 at 21:18
Thank you so much for your time. That is extremely helpful. I'm very much an example driven learner and I find step by step instrucions the best way to understand what stuff does. The problem about swi-prolog documentation is that they only offer the description of each predicate and assume we know how to use them, which I don't. I'm still to find a good place for prolog implementation examples. Even google failed me this time.
– Rui Vaz
Dec 12 '15 at 21:18
add a comment |
There are also a couple of JSON reader/writers around:
Thise modules take atoms and is implemented via DCG:
https://github.com/khueue/prolog-json/tree/master/src
This module is coded directively over ISO streams:
https://gist.github.com/jburse/63986bf525784d6d8cf99db132538d67#file-json_io-p
Both approaches don't require Prolog dicts and are suitable for a wide range of Prolog systems.
add a comment |
There are also a couple of JSON reader/writers around:
Thise modules take atoms and is implemented via DCG:
https://github.com/khueue/prolog-json/tree/master/src
This module is coded directively over ISO streams:
https://gist.github.com/jburse/63986bf525784d6d8cf99db132538d67#file-json_io-p
Both approaches don't require Prolog dicts and are suitable for a wide range of Prolog systems.
add a comment |
There are also a couple of JSON reader/writers around:
Thise modules take atoms and is implemented via DCG:
https://github.com/khueue/prolog-json/tree/master/src
This module is coded directively over ISO streams:
https://gist.github.com/jburse/63986bf525784d6d8cf99db132538d67#file-json_io-p
Both approaches don't require Prolog dicts and are suitable for a wide range of Prolog systems.
There are also a couple of JSON reader/writers around:
Thise modules take atoms and is implemented via DCG:
https://github.com/khueue/prolog-json/tree/master/src
This module is coded directively over ISO streams:
https://gist.github.com/jburse/63986bf525784d6d8cf99db132538d67#file-json_io-p
Both approaches don't require Prolog dicts and are suitable for a wide range of Prolog systems.
answered Nov 22 '18 at 17:03
Harry StotelesHarry Stoteles
292
292
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34242458%2fprolog-receiving-json-post%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I find swi-prolog documentation very good! Maybe you should read Anne Ogborn's excellent tutorial!
– gniourf_gniourf
Dec 12 '15 at 17:26
Thanks for the tip. I tried reading that tutorial but I coudnl't find any information regarding json requests and replies...
– Rui Vaz
Dec 12 '15 at 21:15