Http Request

Discussions related to Visual Prolog
vjoer
Active Member
Posts: 29
Joined: 7 Feb 2006 13:42

Http Request

Unread post by vjoer » 31 Jan 2019 7:54

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
Viorel Farcas

User avatar
Thomas Linder Puls
VIP Member
Posts: 1622
Joined: 28 Feb 2000 0:01

Re: Http Request

Unread post by Thomas Linder Puls » 31 Jan 2019 15:48

This is done in the Vip8 Commercial Edition examples in <examples>\vewService\jsonRpcClient.

See also Web_Services#Visual_Prolog_Client.
Regards Thomas Linder Puls
PDC

vjoer
Active Member
Posts: 29
Joined: 7 Feb 2006 13:42

Re: Http Request

Unread post by vjoer » 1 Feb 2019 7:33

Hello Thomas

I am using 7.3. Can I use the example in 7.3?

Thank you
Viorel Farcas

User avatar
Thomas Linder Puls
VIP Member
Posts: 1622
Joined: 28 Feb 2000 0:01

Re: Http Request

Unread post by Thomas Linder Puls » 1 Feb 2019 10:11

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:

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

User avatar
Tonton Luc
VIP Member
Posts: 501
Joined: 16 Oct 2001 23:01

Re: Http Request

Unread post by Tonton Luc » 12 Feb 2019 13:53

Hi,

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(),

User avatar
Thomas Linder Puls
VIP Member
Posts: 1622
Joined: 28 Feb 2000 0:01

Re: Http Request

Unread post by Thomas Linder Puls » 13 Feb 2019 9:55

Thanks for sharing.

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),         ...
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).
Regards Thomas Linder Puls
PDC

User avatar
Tonton Luc
VIP Member
Posts: 501
Joined: 16 Oct 2001 23:01

Re: Http Request

Unread post by Tonton Luc » 26 Feb 2019 14:29

Hi,

Is it possible to use msglay_Connect with HTTPS or not ?

User avatar
Thomas Linder Puls
VIP Member
Posts: 1622
Joined: 28 Feb 2000 0:01

Re: Http Request

Unread post by Thomas Linder Puls » 26 Feb 2019 15:17

Not.
Regards Thomas Linder Puls
PDC

Post Reply