***********************************************************
function webservicerest(CFILE,cfileresponse,metodo_,pdf_xml)
***********************************************************
Response:=""
if .not. file(Cfile)
msginfo("File not found")
return .f.
endif
Ctext:=MEMOREAD(cfile)
cTextformato:="d?e="+parameter1+"&id="+parameter2+"&t="+parameter3+"&d="+parameter1 && example for send 4 parameters in method GET
ctamano=alltrim(str(len(cText),10))
tamano=len(cText)
UsERNAME:= "xxxxxxxx"
PASSWORD:= "xxxxxxxx"
username64:=hb_base64Encode(username)
password64:=hb_base64Encode(":"+password)
mAuthorization:=hb_base64Encode(username+":"+password)
doc = CreateObject( "MSXML2.DOMDocument" )
ohttp = CreateObject( "MSXML2.XMLHTTP" )
If (Empty( ohttp))
MsgStop( 'Objet Create Error. ' )
Return( .f. )
Endif
if metodo_="POST"
ohttp:Open( "POST" ,"
https://yourdomain.com/xxxxxx/xxxx/yourmethod",.F.)
ELSE
ohttp:Open( "GET" ,"
https://yourdomain.com/xxxxxx/xxxx/yourmethod"+cTextformato,.F.)
ENDIF
ohttp:SetRequestHeader("content-type", "application/json" )
ohttp:SetRequestHeader("Connection", "100-continue" )
ohttp:SetRequestHeader("Expect", "Keep-Alive" )
ohttp:SetRequestHeader("authorization", "Basic "+mAuthorization )
ohttp:SetRequestHeader("content-length",tamano)
if metodo_="POST"
ohttp:Send(Ctext)
else
ohttp:Send(Ctextformato)
endif
mstatus=ohttp:STATUS
if ohttp:STATUS = 200
response:=ohttp:responseText
else
MsgStop( "Error: Status "+str(mstatus,10) )
endif
if (Empty( response ) )
MsgStop( "Error: Respuesta esta VACIA. Status "+str(mstatus,10) )
return
End
Memowrit( cfileresponse,Response)
nLen:=0
res:={}
o=0
nLen := hb_jsondecode( response, @res )
if nlen=0
msginfo("No data found")
return
endif
if metodo_="POST"
**** example for response in format json received
id :=res["Id"]
CUFE :=res["Cufe"]
description :=res["Description"]
qrcode :=res["QrCode"]
state :=res["State"]
validate :=res["Validated"]
result :=res["Result"]
ELSEIF metodo_="GET"
**** example for response in format json received
cresult :=res["Result"]
cdescrip :=res["Description"]
if cresult#0
msginfo(Cdescrip)
ELSE
cDocum :=res["Document"] && here receive a string for convert to pdf or xml
IF TYPE('cDocum')="U"
msginfo(description)
ELSE
IF LEN(ALLTRIM(cdocum))=0
msginfo(" XML FILE is empty")
ENDIF
mdecode:=hb_base64Decode(cDocum) && convert string to pdf or xml
hb_Memowrit("newfile"+'.'+pdf_xml,mdecode)
ENDIF
ENDIF
else
msginfo("Method invalid. ---> get o post")
endif
ohttp := NIL
Release ohttp
return