How to use CURL (HBCURL) with modharbour

Responder
Avatar de Usuario
ricardo arraes
Mensajes: 87
Registered for: 3 years 5 months
Brazil

How to use CURL (HBCURL) with modharbour

Mensaje por ricardo arraes »

Hey everybody,

I'm leaving here a simple explanation of how to use CURL with modharbour:

STEP 1: Attach the hbcurl.ch file to your project
*You can find it right here:
https://github.com/harbour/core/tree/ma ... rib/hbcurl

STEP 2: Include it in your prg file like this

Código: Seleccionar todo

#include "..\include\hbcurl.ch"
STEP 3: Add this code, so you can send a HTTP request through CURL

Código: Seleccionar todo

  curl_global_init()

  if ! empty( hCurl := curl_easy_init() )
    
    //If there's an authorization token, you attach it to the header like this:
    curl_easy_setopt( hCurl, HB_CURLOPT_HTTPHEADER, {"Authorization: "+cHeader} )

    //Set the URL:
    curl_easy_setopt( hCurl, HB_CURLOPT_URL, "http://example.com" )  
    
    //Disabling the SSL peer verification (you can use it if you have no SSL certificate yet, but still want to test HTTPS)
    curl_easy_setopt(hCurl, HB_CURLOPT_FOLLOWLOCATION, 1)
    curl_easy_setopt(hCurl, HB_CURLOPT_SSL_VERIFYPEER, 0)

    //If you are sending a POST method request, you gotta attach your fields with this clause using
    //url-encoded pattern
    //If you are sending a GET method request, you can just delete this clause, because your parameters will be attached 
    //directly into your URL
    curl_easy_setopt( hCurl, HB_CURLOPT_POSTFIELDS, "example=param&example2=param2...")          

    //Setting the buffer
    curl_easy_setopt( hCurl, HB_CURLOPT_DL_BUFF_SETUP )
    
    //Sending the request and getting the response
    IF (nret:=curl_easy_perform( hCurl )) == 0
        uValue := curl_easy_dl_buff_get( hCurl )
    ENDIF
  ENDIF
  
  //Cleaning the curl instance
  curl_global_cleanup()   

  //I'm using hb_jsonDecode() so I can decode the responde into a JSON object
  hb_jsonDecode(uValue)  
The work always comes before the belief

Avatar de Usuario
charly
Mensajes: 145
Registered for: 3 years 6 months

Mensaje por charly »

Ricardo,

Perfecto ! gracias :-)


Un ejemplo sencillo de 2 lineas de como llamar a un servicio ?...
Salutacions, saludos, regards.
Charly

"...programar es fácil, hacer programas es difícil..."

https://httpd2.blogspot.com/
https://forum.modharbour.app

Avatar de Usuario
ricardo arraes
Mensajes: 87
Registered for: 3 years 5 months
Brazil

Mensaje por ricardo arraes »

por supuesto!

Let's say that we got a webservice running on:
www.mywebservice.com/ws

Now, I need to request some information from it:

Código: Seleccionar todo

#include "..\include\hbcurl.ch"

FUNCTION sendHttp()

  LOCAL hCurl,uValue, hResponse:={=>}
  LOCAL cHeader:="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"

  curl_global_init()

  if ! empty( hCurl := curl_easy_init() )
   
    curl_easy_setopt( hCurl, HB_CURLOPT_HTTPHEADER, {"Authorization: "+cHeader} )

    curl_easy_setopt( hCurl, HB_CURLOPT_URL,  "www.mywebservice.com/ws?param1=info1&param2=info2&param3=info3")  
    curl_easy_setopt( hCurl, HB_CURLOPT_DL_BUFF_SETUP )
    
    IF (nret:=curl_easy_perform( hCurl )) == 0
        uValue := curl_easy_dl_buff_get( hCurl )
    ENDIF
  ENDIF
  
  curl_global_cleanup()   
  
  hResponse:=  hb_jsonDecode(uValue)  

RETURN hResponse


Now, we need to send a post method to the webservice in order to save something:

Código: Seleccionar todo

#include "..\include\hbcurl.ch"

FUNCTION sendHttp()

  LOCAL hCurl,uValue, hResponse:={=>}
  LOCAL cHeader:="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"

  curl_global_init()

  if ! empty( hCurl := curl_easy_init() )
    
    curl_easy_setopt( hCurl, HB_CURLOPT_HTTPHEADER, {"Authorization: "+cHeader} )
    

    curl_easy_setopt( hCurl, HB_CURLOPT_URL,  "www.mywebservice.com/ws")  

    curl_easy_setopt( hCurl, HB_CURLOPT_POSTFIELDS, "param1=info1&param2=info2&param3=info3)          

    curl_easy_setopt( hCurl, HB_CURLOPT_DL_BUFF_SETUP )
    
    IF (nret:=curl_easy_perform( hCurl )) == 0
        uValue := curl_easy_dl_buff_get( hCurl )
    ENDIF
  ENDIF
  
  curl_global_cleanup()   
  
  hResponse:=hb_jsonDecode(uValue)  

RETURN hResponse


I'm not sure if that's what you asked for, Charly, but I hope it helps anyway... :D
The work always comes before the belief

Avatar de Usuario
charly
Mensajes: 145
Registered for: 3 years 6 months

Mensaje por charly »

Ricardo,

Perfect Ricardo, in this way users can better understand how to use this contribution :D
Salutacions, saludos, regards.
Charly

"...programar es fácil, hacer programas es difícil..."

https://httpd2.blogspot.com/
https://forum.modharbour.app

Responder