Hello
I need to send a HTTP request (GET) to a URL and get the response (JSON). I havent found such example in the forum.
Could you give such example?
Thank you
Regards
-
- VIP Member
- Posts: 1466
- Joined: 28 Feb 2000 0:01
Re: Http Request
This is done in the Vip8 Commercial Edition examples in <examples>\vewService\jsonRpcClient.
See also Web_Services#Visual_Prolog_Client.
See also Web_Services#Visual_Prolog_Client.
Regards Thomas Linder Puls
PDC
PDC
-
- Posts: 18
- Joined: 7 Feb 2006 13:42
-
- VIP Member
- Posts: 1466
- Joined: 28 Feb 2000 0:01
Re: Http Request
Some of it is most likely usefull in Vip7.3, but other things you will have to do yourself.
The essential part of the example is here:
The essential part of the example is here:
Code: Select all
predicates
httpPOST : (string Method, jsonObject Params).
clauses
httpPOST(Method, Params) :-
RequestObject = jsonRpcRequest::newNextId(),
RequestObject:method := Method,
RequestObject:params := some(json::o(Params)),
BaseURL = serverUrl_ctl:getText(),
textStatusCell:text := string::format("% ...", BaseUrl),
URL = string::format("%/jsonRpc", BaseUrl),
RequestString = RequestObject:asString(),
stdio::writef("POST %\n", RequestString),
% delayCall to empty the message queue, so the message control receives a WM_PAINT event
delayCall(1, { :- httpPOST2(Url, RequestString) }).
predicates
httpPOST2 : (string Url, string RequestString).
clauses
httpPOST2(URL, RequestString) :-
try
XmlHttp = xmlhttp60::new(),
XmlHttp:open_predicate("POST", URL, comDomains::boolean(false), comDomains::null, comDomains::null),
XmlHttp:setRequestHeader("Content-Type", "application/json-rpc; charset=UTF-8"),
XmlHttp:send(comDomains::string(RequestString)),
handleResponse(XmlHttp)
catch TraceId do
if ComException = exception::tryGetDescriptor(TraceId, exceptionHandling_exception::genericComException) and
unsigned(0x800C0005) = exception::tryGetExtraInfo(ComException, exceptionHandling_exception::hResult_parameter)
then
stdio::writef("Cannot connect to %\n", Url)
else
ExtraInfo = [namedValue("request", string(RequestString)), namedValue("url", string(Url))],
exception::continueDetailed(TraceId, exception::unknown, ExtraInfo)
end if
finally
textStatusCell:text := "",
setAllEnabled(true)
end try.
class predicates
handleResponse : (xmlhttp60 XmlHttp).
clauses
handleResponse(XmlHttp) :-
Hdrs = XmlHttp:getAllResponseHeaders(),
Resp = XmlHttp:responseText,
JsonResp = jsonObject::fromString(Resp),
stdio::writef("HEADERS:\n%\n", Hdrs),
if Error = JsonResp:tryGet_object("error") then
if Code = Error:tryGet_integer("code") then
stdio::writef("Code: %\n", Code)
end if,
if Msg = Error:tryGet_string("message") then
stdio::writef("Message: %\n", Msg)
end if,
if Data = Error:tryGet_string("data") then
stdio::writef("Data: %\n", Data)
else
stdio::writef("Error: %\n", Error:asString())
end if
elseif Result = JsonResp:tryGet_object("result") then
stdio::writef("Result: %\n", Result:asString())
else
stdio::writef("Unexpected response: %\n", Resp)
end if.
Regards Thomas Linder Puls
PDC
PDC
-
- VIP Member
- Posts: 204
- Joined: 16 Oct 2001 23:01
Re: Http Request
Hi,
Here is a VP 7.3 code sample :
Here is a VP 7.3 code sample :
Code: Select all
serveur = "your_server.com",
Req = format("GET http://%/your_folder/your_php_file.php?x=%\n\n",serveur,Action),
trap(SOCKET = msgSocketBlocking::msglay_Connect(serveur, 80, []),_,(stdio::write("msglay_Connect : not ok\n"),fail())),
msglay_WriteStr(SOCKET,Req),
stdio::write(""),
msgSocketBlocking::msglay_read(SOCKET,100,Event_ReadBin),
Event_ReadBin = e_ReadBin(ReponseBin),
SL = binary::binary8ToStringList(ReponseBin),
SL = [Reponse|_],
stdio::write("Respons = ",Reponse),stdio::nl,
msglayer::msglay_Close(SOCKET),
clear_msgl_socket_eventqueue_db(SOCKET),
msgSocketBlocking::msglay_CloseSession(),
-
- VIP Member
- Posts: 1466
- Joined: 28 Feb 2000 0:01
Re: Http Request
Thanks for sharing.
May I suggest that you use try-catch try instead of the trap/3 predicate:
This is of course example code, but for "production" code I will recommend that you don't throw away the exception trace; sometimes you actually get good error messages from Windows. (See exception handling).
You should notice that this code will not work for secure connections (i.e. https).
May I suggest that you use try-catch try instead of the trap/3 predicate:
Code: Select all
serveur = "your_server.com",
Req = format("GET http://%/your_folder/your_php_file.php?x=%\n\n", serveur, Action),
try
SOCKET = msgSocketBlocking::msglay_Connect(serveur, 80, [])
catch _ do
stdio::write("msglay_Connect : not ok\n"),
fail()
end try,
msglay_WriteStr(SOCKET, Req),
...
You should notice that this code will not work for secure connections (i.e. https).
Regards Thomas Linder Puls
PDC
PDC
-
- VIP Member
- Posts: 204
- Joined: 16 Oct 2001 23:01
Re: Http Request
Hi,
Is it possible to use msglay_Connect with HTTPS or not ?
Is it possible to use msglay_Connect with HTTPS or not ?
-
- VIP Member
- Posts: 1466
- Joined: 28 Feb 2000 0:01