PC SOFT

PROFESSIONAL NEWSGROUPS
WINDEVWEBDEV and WINDEV Mobile

Home → WINDEV 2024 → API (REST) Request
API (REST) Request
Started by Kim O'Regan, Dec., 05 2019 11:32 AM - 6 replies
Posted on December, 05 2019 - 11:32 AM
Hi all,

I'm trying to figure out how to create an API-request in Windev.

The request looks like this:
<?xml version='1.0' encoding='ISO-8859-1'?>
<marathon>
<password>abc123</password>
<type>api_operation</type>
<company_id>ABC</company_id>
<employee_id>ABC123</employee_id>
<date>2019-12-04</date>
<repeat_until></repeat_until>
<client_id>ABC</client_id>
<project_no>123</project_no>
<feecode_id>123</feecode_id>
<hours>4,5</hours>
<full_time_percentage></full_time_percentage>
<comment>ABC-1234 Working</comment>
</marathon>

Which is posted to:
"https://adress_of_webservice"

How would I do this in Windev?

Thankful for any help

Regards
/Kim
Posted on December, 05 2019 - 1:01 PM
Try this:

sXml is string = [
<?xml version='1.0' encoding='ISO-8859-1'?>
<marathon>
<password>abc123</password>
<type>api_operation</type>
<company_id>ABC</company_id>
<employee_id>ABC123</employee_id>
<date>2019-12-04</date>
<repeat_until></repeat_until>
<client_id>ABC</client_id>
<project_no>123</project_no>
<feecode_id>123</feecode_id>
<hours>4,5</hours>
<full_time_percentage></full_time_percentage>
<comment>ABC-1234 Working</comment>
</marathon>
]


Req is httpRequest
cReq.URL = https://adress_of_webservice"
cReq..ContentType = "application/xml" //or "text/xml"
cReq..Method = httpPost
cReq..Content = sXml
//cReq.Header["x-auth-token"] = "???" //optional: sometimes needed for authentication

cResp is httpResponse = HTTPSend(cReq)

SWTICH cResp..StatusCode
CASE 200
//OK
OTHER CASE
//cResp..Content contains more info
END
Posted on December, 05 2019 - 3:13 PM
Hi and thanks for the answer Jeroen.
Your example worked when I put in the XML-request in the sXml brackets.
The "problem" is that I will be reading rows from a database and sending them with httpPost.
So some of the values has to be variables. How do I achieve that?

Functioning:
sXml is string = [
<?xml version='1.0' encoding='ISO-8859-1'?>
<marathon>
<password>abc123</password>
<type>api_operation</type>
<company_id>ABC</company_id>
<employee_id>ABC123</employee_id>
<date>2019-12-04</date>
<repeat_until></repeat_until>
<client_id>ABC</client_id>
<project_no>123</project_no>
<feecode_id>123</feecode_id>
<hours>4,5</hours>
<full_time_percentage></full_time_percentage>
<comment>ABC-1234 Working</comment>
</marathon>
]

cReq is restRequest
cReq.URL = "https://webservice_server"
cReq..ContentType = "application/xml" //or "text/xml"
cReq..Method = httpPost
cReq..Content = sXml
//cReq.Header["x-auth-token"] = "???" //optional: sometimes needed for authentication

cResp is restResponse = HTTPSend(cReq)

Regards
/Kim
Posted on December, 06 2019 - 2:20 PM
Try this:

xmlDoc is xmlDocument
xmlDoc..Encoding = XMLEncodingUTF8

xmlDoc.marathon.password = "abc123"
xmlDoc.marathon.type = "api_operation"
xmlDoc.marathon.company_id = "ABC"
xmlDoc.marathon.employee_id = "ABC123"
xmlDoc.marathon.date="2019-12-04"
xmlDoc.marathon.repeat_until = ""
xmlDoc.marathon.client_id="ABC"
xmlDoc.marathon.project_no=123
xmlDoc.marathon.feecode_id=123
xmlDoc.marathon.hours=4.5
xmlDoc.marathon.full_time_percentage=""
xmlDoc.marathon.comment="ABC-1234 Working"

cReq is httpRequest
cReq.URL = "https://adress_of_webservice"
cReq..ContentType = "application/xml" //or "text/xml"
cReq..Method = httpPost
cReq..Content = XMLBuildString(xmlDoc,XMLDocumentDefault)
//cReq.Header["x-auth-token"] = "???" //optional: sometimes needed for authentication

cResp is httpResponse = HTTPSend(cReq)

SWITCH cResp..StatusCode
CASE 200
//OK
OTHER CASE
//cResp..Content contains more info
END
Posted on December, 06 2019 - 4:46 PM
Hi and thanks for the answer, again, Jeroen!

I will try this.

/Kim
Posted on December, 09 2019 - 4:36 PM
Hi Jeroen,
I get this when running the script:





"Failure retrieving the XML.
unknown encoding 2
.content"

Tried the "text/xml" to no prevail.

The Content seems to be empty.

What's missing?

Regards
/Kim
Posted on December, 10 2019 - 7:01 PM
Hi,
I've managed to implement the XML-part into my application. So now it sends and stores the info in the database.
I do though have a "small" problem. I use time in my application which is formatted hh:mm.
The recieving system use either hh,mm or hh.mm and, e.g 1:30 is displayed 1,5 or 1.5 on the recieving system.
How do I convert e.g 1:30 to 1,5?

Regards
/Kim