Welcome! Log In Create A New Profile

Advanced

WINDEV restResponse explanation

Posted by SteveSitas 
WINDEV restResponse explanation
December 10, 2020 06:59PM
Hi,
I am having some difficulties understanding PCSofts help concerning restResponse.
According to the help: Header is an Associative array of character strings that receives the Key/Value set of headers.

Here is the help topic: help topic

Does anybody have any code examples on this?

* Using WD25 and a 3rd party Rest API.

Regards
Steven Sitas
Re: WINDEV restResponse explanation
December 11, 2020 08:55AM
Hi Steve

Guess you know "Associative Arrays" so it is easy.
If the Rest Response provides you for example a security token back via thr header you can get it the same way as you do for an associative array item.
//Declare
RstRqt is RestRequest 
RstRsp is RestResponse 
JRequest is JSON <description="yourinputjsonsamplefilename.json">
JResponse is JSON <description="youroutputjsonsamplefilename.json">

//Define
...Set your request and json items in RstRqt...
JRequest.Item= ...
RstRqst..Content=JRequest
...

//Execute
RstRsp = RestSend(RstRqt)

//Result
If RstRsp..StatusCode = 200 then
   SecToken is string = RstRsp..Header["Token"]
   JResponse = RstRsp..Content
else
...Error handling...
end

That's it

Cheers

Peter Holemans



Edited 2 time(s). Last edit at 12/11/2020 10:50AM by PeHoBe.
Re: WINDEV restResponse explanation
December 11, 2020 10:18AM
Hi Peter,

thank you very much for your code - works like a charm ...
The help, referring to associative arrays was a little confusing !!!

Before you posted your answer I did a little research and here is what I found:
If you are getting back (.Response) an XML string you can transform the XML to a JSON, which is a lot easier to handle.
You can find the code here: [blogs.pcsoft.fr]

I used Google translate from French->English _AND_ did a simple addition in the procedure. It wasn't returning the JSON object.
And always don't forget to use: sResult is string=UTF8ToUnicode(cMyResponse..Content) before using the sResult

Thanks again Peter

Regards
Steven Sitas
Re: WINDEV restResponse explanation
December 11, 2020 10:46AM
Hi Steve

You're welcome!

Two more notes however:
1) For a lot of REST API type web services you can provide the format it needs to reply in (json or xml). This is done via the ..ContentType attribute in your RestRequest. In general json has the preference as it is more lightweight (e.g. mobile devices and mobile connections).

2) By using the <description=...> attribute during declaration you can directly work with the response instead of having to move it to a string first. I believe any conversion in encoding is done explicitely when doing it that way. In order to do so, you simply need to add your json files into the "External Descriptions" section within your project. Next you can drag and drop it from there into your code editor and the declaration is made automatically. The same can be done for nodes within your structure etc. Works a 1000-times easier and you get all the benefits of type-ahead and assignment errors in the editor preventing runtime issues in your final product.

Cheers

Peter Holemans



Edited 2 time(s). Last edit at 12/11/2020 10:51AM by PeHoBe.
Re: WINDEV restResponse explanation
December 11, 2020 11:11AM
Hi Peter,

unfortunately, the RESTServices that I use - from the GREEK TAX Authority - do not support JSON (yet).

Regards
Steven Sitas
Re: WINDEV restResponse explanation
December 11, 2020 12:03PM
Hi Steven

Understood.
Just keep in mind the same logic as with JSON content applies with your declarations on XML documents of course...
E.g.:
XResponse is XMLDocument <description="yoursamplexmlresponsefile">
XResponse = RstRsp..Content

Next you'll have the same type-ahead and assignment features in the code editor as with JSON.

And next you can easily break it up into it's nodes.
XRespNode is XMLNode <description="yorusamplexmlresponsefile.node1.node2">

FOR EACH XRespNode OF XResponse
SomeValue=XRespNode.SomeXMLField
END

etc...

Simply add your sample XML files into the External Descriptions section of your project.

Cheers

Peter
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: