HTTPS post request

253 views
Skip to first unread message

Attila Szabó

unread,
Nov 4, 2025, 3:27:15 AMNov 4
to Harbour Users
Hi,

I would like to replace the following external program call with program code written purely in Harbour:

HB_RUN( 'curl.exe --tlsv1.2 -X POST --connect-timeout 6 -H "Accept: application/xml" -H "Content-Type: application/xml" -d @C:\Temp\Request.xml -o C:\Temp\Response.xml -s https://api-test.xyz.com/xService' )

Can it be solved by using HBTIP + HBSSL?

I would be interested in any existing solutions with a short example code.
Thank you very much.

Regards,
Attila

Gerald Drouillard

unread,
Nov 4, 2025, 8:47:35 AMNov 4
to harbou...@googlegroups.com
why not use hb_curl lib?

--
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: https://groups.google.com/group/harbour-users
---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/harbour-users/f015dc58-daba-449b-8abe-96c17bbc3acfn%40googlegroups.com.

Daniel Aisenberg

unread,
Nov 4, 2025, 9:45:25 AMNov 4
to harbou...@googlegroups.com
url := "https://api-test.xyz.com/xService"
sRequest := memoread( "c:\temp\Request.xml" )
oXml := Win_OleCreateObject( 'MSXML2.XMLHTTP' )
oXml:Open("POST",url,.f.)
oXml:SetRequestHeader("Content-Type", "no-cache")
oXml:setRequestHeader( "SOAPAction:", "None" )
oXml:setRequestHeader( "Content-Type", "text/xml;charset=UTF-8" )
oXml:Send(sRequest)
sResponse := oXml:responseText
nHand:=fcreate( "C:\Temp\Response.xml", 0)
fwrite( nHand, sResponse )
fclose( nHand )
oXml:=NIL
release oXml


--

Attila Szabó

unread,
Nov 4, 2025, 1:47:05 PMNov 4
to Harbour Users
Hi Daniel,

I tried the code, but it didn't work as expected.
The server sends an error message:
"<html><head><title>Error</title></head><body>Bad Request</body></html>"
I think it would be important to set the communication type to TLS v1.2.
And probably the "Accept: application/xml" setting as well.


Attila Szabó

unread,
Nov 4, 2025, 1:51:58 PMNov 4
to Harbour Users
Hi Gerald,

I haven't used it yet. It seems complicated. But it's likely that the hb_curl lib will provide the perfect solution.
Can you send me some code for this case?

Best regards:
Attila

Max Thai

unread,
Nov 4, 2025, 1:57:31 PMNov 4
to harbou...@googlegroups.com
function webservicerest(anexo,cfileresponse,metodo_,pdf_xml,urlfinal,urlcompleta)
***********************************************************
local XConsumerKey := cap_iniN('C','WEBMANIA','X-Consumer-Key')
local XConsumerSecret := cap_iniN('C','WEBMANIA','X-Consumer-Secret')
local XAccessToken := cap_iniN('C','WEBMANIA','X-Access-Token')
local XAccessTokenSecret := cap_iniN('C','WEBMANIA','X-Access-Token-Secret')
local Cookie := cap_iniN('C','WEBMANIA','Cookie')
local Response:=""


        if !IsConnected()
                msgstop('Parece que seu computador está sem internet!'+quebra+quebra+'Módulo NFCe inativo!','NFCe inativo')
                return
        endif


REQUEST HB_CODEPAGE_UTF8

        hb_cdpSelect( "UTF8" )

        REQUEST HB_CODEPAGE_PTISO

        HB_CDPSelect("PTISO")

        Default  urlcompleta  To ''

        waitwindow('Consultando api NFCE... Aguarde!',.t.)


//cTextformato:="d?e="+parameter1+"&id="+parameter2+"&t="+parameter3+"&d="+parameter1  && example for send 4 parameters in method GET
cTextformato:=''
cText := anexo

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" )
oHTTP := CreateObject( "MSXML2.ServerXMLHTTP.6.0" )
If (Empty( ohttp))
   MsgStop( 'Objet Create Error. ' )
   Return( .f. )
Endif

// Definir o timeout aqui (exemplo: 1 minutos para receber resposta)
//oHTTP:setTimeouts(120000, 1200000, 1200000, 120000)

if metodo_="POST"
        if len(urlcompleta)=0
                ohttp:Open( "POST" ,"https://webmaniabr.com/api/1/nfe/"+urlfinal+"/",.T.)
        else  
                ohttp:Open( "POST" ,urlcompleta,.F.)
        endif                        
ELSEIF metodo_='GET'
        if len(urlcompleta)=0
           ohttp:Open( "GET" ,"https://webmaniabr.com/api/1/nfe/"+urlfinal+"/"+cTextformato,.T.)
       
        else
           ohttp:Open( "GET" ,urlcompleta,.F.)
        endif                  
ELSEIF metodo_='PUT'
        if len(urlcompleta)=0
           ohttp:Open( "PUT" ,"https://webmaniabr.com/api/1/nfe/"+urlfinal+"/"+cTextformato,.T.)
        else
           ohttp:Open( "PUT" ,urlcompleta,.F.)
        endif                  
ENDIF

ohttp:SetRequestHeader("Cache-Control", "no-cache" )
ohttp:SetRequestHeader("X-Consumer-Key", XConsumerKey )
ohttp:SetRequestHeader("X-Consumer-Secret", XConsumerSecret )
ohttp:SetRequestHeader("X-Access-Token", XAccessToken )
ohttp:SetRequestHeader("X-Access-Token-Secret", XAccessTokenSecret )
//ohttp:SetRequestHeader("Cookie", Cookie )
ohttp:SetRequestHeader("content-length",tamano)


if pdf_xml='PDF'
        ohttp:SetRequestHeader("content-type", "application/pdf" )
elseif pdf_xml='JSON'        
        ohttp:SetRequestHeader("content-type", "application/json" )
elseif pdf_xml='XML'        
        ohttp:SetRequestHeader("content-type", "text/xml" )
endif
               
if metodo_="POST"

        BEGIN SEQUENCE WITH {|e| NIL}
              ohttp:Send(anexo)
           RECOVER USING oErr
              // Se der erro já no Send (ex.: timeout de conexão), retorne vazio sem quebrar
                msgstop('Falhou comunicação com a sefaz, atividade não realizada!','ATENÇÃO')
              RETURN
           END SEQUENCE          
                     
else
           
        BEGIN SEQUENCE WITH {|e| NIL}
              ohttp:Send(anexo)
           RECOVER USING oErr
              // Se der erro já no Send (ex.: timeout de conexão), retorne vazio sem quebrar
                msgstop('Falhou comunicação com a sefaz, atividade não realizada!','ATENÇÃO')
              RETURN
           END SEQUENCE          
                     
endif
oHTTP:WaitForResponse( 2000 )
           
mstatus=ohttp:STATUS
if ohttp:STATUS = 200
    response:=ohttp:responseText
else
//   response:=ohttp:responseText
   MsgStop( "Error: Status "+str(mstatus,10) )
endif

        waitwindow()

   
   if (Empty( response ) )
   MsgStop( "Erro:  Resposta vazia.  Status "+str(mstatus,10) )
   return
End
//hb_Memowrit( cfileresponse,Response)
SalvaTextoPuro( Response,  cfileresponse)                
           
response := hb_memoread(cFileresponse)


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
//      error               :=res["error"]
//      error := arrumautf8(error)          
//      hb_memowrit('retorno.txt',error)          
ELSEIF metodo_="GET"
 
   ****   example for response in format json received
     cDocum   := response   &&  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
//        SalvaTextoPuro( cDocum,  'arquivo.xml')                
        mdecode:=hb_base64Decode(cDocum)               && convert string to pdf or xml
  //      SalvaTextoPuro( mdecode,  'arquivo2.xml')                
//        hb_Memowrit("newfile"+'.'+pdf_xml,response)
        SalvaTextoPuro( response,  "newfile"+'.'+pdf_xml)                

  //      hb_Memowrit("newfile2"+'.'+pdf_xml,cDocum)
 
     ENDIF

else
//   msginfo("Metodo Inválido.  ---> get o post")
endif
ohttp  := NIL
Release ohttp
return response

Eu uso assim, essa função tem bastante sujeira de testes que fiz, mas está funcionando aqui. Espero que ajude.

Attila Szabó

unread,
Nov 5, 2025, 1:47:17 PMNov 5
to Harbour Users
Hi MAX,

What you shared was very helpful.
I was able to solve the problem.
Thank You!

Regards,
Attila

Max Thai

unread,
Nov 5, 2025, 2:02:12 PMNov 5
to harbou...@googlegroups.com
Fico feliz em ter ajudado.

Um forte abraço,

Reply all
Reply to author
Forward
0 new messages