Page 1 of 1

Http Request

Posted: 31 Jan 2019 7:54
by vjoer
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

Re: Http Request

Posted: 31 Jan 2019 15:48
by Thomas Linder Puls
This is done in the Vip8 Commercial Edition examples in <examples>\vewService\jsonRpcClient.

See also Web_Services#Visual_Prolog_Client.

Re: Http Request

Posted: 1 Feb 2019 7:33
by vjoer
Hello Thomas

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

Thank you

Re: Http Request

Posted: 1 Feb 2019 10:11
by Thomas Linder Puls
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.

Re: Http Request

Posted: 12 Feb 2019 13:53
by Tonton Luc
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(),

Re: Http Request

Posted: 13 Feb 2019 9:55
by Thomas Linder Puls
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).

Re: Http Request

Posted: 26 Feb 2019 14:29
by Tonton Luc
Hi,

Is it possible to use msglay_Connect with HTTPS or not ?

Re: Http Request

Posted: 26 Feb 2019 15:17
by Thomas Linder Puls
Not.