FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour CURL.EXE to LIBCURL (SOLVED)
Posts: 115
Joined: Wed Oct 26, 2005 02:38 PM
CURL.EXE to LIBCURL (SOLVED)
Posted: Thu May 20, 2021 11:11 PM
Hey guys.

Code (fw): Select all Collapse
curl --location --request POST "https://sandbox.gerencianet.com.br/v1/authorize" --header "Authorization: Basic Q2..." --header "Content-Type: application/json" --data-raw "{  \"grant_type\":\"client_credentials\"}"


I'm trying to convert a CURL.EXE command to LIBCURL, I have the example below in C.

Code (fw): Select all Collapse
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
  curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
  curl_easy_setopt(curl, CURLOPT_URL, "https://sandbox.gerencianet.com.br/v1/authorize");
  curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
  struct curl_slist *headers = NULL;
  headers = curl_slist_append(headers, "Authorization: Basic Q2...");
  headers = curl_slist_append(headers, "Content-Type: application/json");
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  const char *data = "{\n    \"grant_type\": \"client_credentials\"\n}";
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
  res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);


But the command curl_slist_append did not find an equivalent in lib using xHarbour.

Can someone help?

Thanks
Christiano Augusto Silveira
christiano.silveira@gmail.com

MaxxTech Soluções em TI
http://www.maxxtech.com.br
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: CURL.EXE to LIBCURL
Posted: Sun May 23, 2021 07:14 AM
Christiano, try with this

Code (fw): Select all Collapse
local aHeaders  := {}
...
      AAdd( aHeaders, "accept: application/json" )
      AAdd( aHeaders, "Content-Type: application/json" )
      curl_easy_setopt( hCurl, HB_CURLOPT_HTTPHEADER, aHeaders )
...
Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo

El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 115
Joined: Wed Oct 26, 2005 02:38 PM
Re: CURL.EXE to LIBCURL (SOLVED)
Posted: Wed May 26, 2021 05:09 PM

Thanks for the answer.

I ended up solving it using CURL.EXE and a .BAT.

Christiano Augusto Silveira
christiano.silveira@gmail.com

MaxxTech Soluções em TI
http://www.maxxtech.com.br
Posts: 129
Joined: Mon Oct 17, 2005 03:03 AM
Re: CURL.EXE to LIBCURL
Posted: Sun May 30, 2021 08:20 AM
Hi! Can you translation the curl.exe command?

Code (fw): Select all Collapse
        cPara := [-H "Authorization: Bearer xPGwmsfPrGdAHiCYbb4d63yQ5pXgrbenewLi37h97ix" ]
        cPara += [-H "Content-Type : application/x-www-form-urlencoded; charset=utf-8" ]
        cPara += [-F "imageFile=@e:/temp/01.jpg" ]
        cPara += [-F "message=Hello" ]
        cPara += [https://notify-api.line.me/api/notify ]
        cPara += [>debug.txt]
        //
        Shell32( GetActiveWindow(),, "curl.exe ",cPara,,0) // call curl.exe
line ID: ssbbstw

WeChat ID: ssbbstw
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: CURL.EXE to LIBCURL (SOLVED)
Posted: Sun May 30, 2021 02:32 PM
Try with this

Code (fw): Select all Collapse
#include "Fivewin.ch"
#include "hbcurl.ch"

Function Main()

   local cUrl := "https://notify-api.line.me/api/notify"

   TestCurl( cUrl )

Return nil

Function TestCurl( cUrl )

   local hCurl
   local cError
   local cTexto   := ""
   local aHeaders := {}
   local cParam
   TEXT INTO cParam
    {  
    "imageFile": "d:/fwh/fwhteam/samples/ejemplo.jpg",
    "message": "Hello"
    }
   ENDTEXT
   curl_global_init()
   hCurl := curl_easy_init()
   if !empty( hCurl )
      
      curl_easy_setopt( hCurl, HB_CURLOPT_URL, cUrl )
      AAdd( aHeaders, "Authorization: Bearer xPGwmsfPrGdAHiCYbb4d63yQ5pXgrbenewLi37h97ix" ) //"accept: application/json" )
      AAdd( aHeaders, "Content-Type : application/x-www-form-urlencoded; charset=utf-8" )   //"Content-Type: application/json" )
      curl_easy_setopt( hCurl, HB_CURLOPT_HTTPHEADER, aHeaders )
      curl_easy_setopt( hCurl, HB_CURLOPT_CONNECTTIMEOUT , 100 )
      curl_easy_setopt( hCurl, HB_CURLOPT_POST, 1)
      //curl_easy_setopt( hCurl, HB_CURLOPT_CUSTOMREQUEST, 'POST')
      curl_easy_setopt( hCurl, HB_CURLOPT_POSTFIELDS, cParam )
      curl_easy_setopt( hCurl, HB_CURLOPT_SSL_VERIFYHOST, .F. )
      curl_easy_setopt( hCurl, HB_CURLOPT_SSL_VERIFYPEER, .F. )
      curl_easy_setopt( hCurl, HB_CURLOPT_DL_BUFF_SETUP )

      cError := curl_easy_perform( hCurl )
      if !Empty( cError )
          MsgInfo( curl_easy_strerror( cError ), "Error" )
      endif
      cTexto  := curl_easy_dl_buff_get( hCurl )
      MsgInfo( cTexto )
      curl_easy_reset( hCurl )
   endif
   curl_global_cleanup()

Return nil
Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo

El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 129
Joined: Mon Oct 17, 2005 03:03 AM
Re: CURL.EXE to LIBCURL (SOLVED)
Posted: Sun May 30, 2021 03:06 PM
cnavarro wrote:Try with this

Code (fw): Select all Collapse
#include "Fivewin.ch"
#include "hbcurl.ch"

Function Main()

   local cUrl := "https://notify-api.line.me/api/notify"

   TestCurl( cUrl )

Return nil

Function TestCurl( cUrl )

   local hCurl
   local cError
   local cTexto   := ""
   local aHeaders := {}
   local cParam
   TEXT INTO cParam
    {  
    "imageFile": "d:/fwh/fwhteam/samples/ejemplo.jpg",
    "message": "Hello"
    }
   ENDTEXT
   curl_global_init()
   hCurl := curl_easy_init()
   if !empty( hCurl )
      
      curl_easy_setopt( hCurl, HB_CURLOPT_URL, cUrl )
      AAdd( aHeaders, "Authorization: Bearer xPGwmsfPrGdAHiCYbb4d63yQ5pXgrbenewLi37h97ix" ) //"accept: application/json" )
      AAdd( aHeaders, "Content-Type : application/x-www-form-urlencoded; charset=utf-8" )   //"Content-Type: application/json" )
      curl_easy_setopt( hCurl, HB_CURLOPT_HTTPHEADER, aHeaders )
      curl_easy_setopt( hCurl, HB_CURLOPT_CONNECTTIMEOUT , 100 )
      curl_easy_setopt( hCurl, HB_CURLOPT_POST, 1)
      //curl_easy_setopt( hCurl, HB_CURLOPT_CUSTOMREQUEST, 'POST')
      curl_easy_setopt( hCurl, HB_CURLOPT_POSTFIELDS, cParam )
      curl_easy_setopt( hCurl, HB_CURLOPT_SSL_VERIFYHOST, .F. )
      curl_easy_setopt( hCurl, HB_CURLOPT_SSL_VERIFYPEER, .F. )
      curl_easy_setopt( hCurl, HB_CURLOPT_DL_BUFF_SETUP )

      cError := curl_easy_perform( hCurl )
      if !Empty( cError )
          MsgInfo( curl_easy_strerror( cError ), "Error" )
      endif
      cTexto  := curl_easy_dl_buff_get( hCurl )
      MsgInfo( cTexto )
      curl_easy_reset( hCurl )
   endif
   curl_global_cleanup()

Return nil


Thank you! but show {"status":400,"message":"message: must not be empty"} .... fail!
if:

Code (fw): Select all Collapse
   curl_easy_setopt( hCurl, HB_CURLOPT_POSTFIELDS, cParam )


change to :

Code (fw): Select all Collapse
   curl_easy_setopt( hCurl, HB_CURLOPT_POSTFIELDS, "message=Hello" )


is correct!!
bu image file 'd:/fwh/fwhteam/samples/ejemplo.jpg' not to send!! :-)
line ID: ssbbstw

WeChat ID: ssbbstw
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: CURL.EXE to LIBCURL (SOLVED)
Posted: Sun May 30, 2021 11:22 PM
In order to correctly program a request to an API, it is essential to know the documentation that specifies the type of data and the keys that we can send as well as the meaning of these keys.
Please tell me where I can read the documentation for that API to be able to program it correctly.
On the other hand, you have to put a path and image file that exists on your computer. Obviously that is an example in which I take a file on my computer with the paths that I use.
Another additional issue is that I can't test it properly because the message I get is: Invalid access token.
As for the message that you get, you should try in my code to put it like this:
Notice that I have removed the quotes in the message ("")
Please put in "imageFile" a path and a file that exists on your computer.

TEXT INTO cParam
{
"imageFile": "d:/fwh/fwhteam/samples/ejemplo.jpg",
"message": Hello
}

You have to look at the API documentation to give the parameters that are sent the format expected by the API. You will have to try.
Perhaps the file name does not need to put the quotation marks ("") either
Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo

El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 129
Joined: Mon Oct 17, 2005 03:03 AM
Re: CURL.EXE to LIBCURL (SOLVED)
Posted: Mon May 31, 2021 01:50 AM
line ID: ssbbstw

WeChat ID: ssbbstw

Continue the discussion