Page 1 of 1

CURL.EXE to LIBCURL (SOLVED)

PostPosted: Thu May 20, 2021 11:11 pm
by ctoas
Hey guys.

Code: Select all  Expand view
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: Select all  Expand view
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

Re: CURL.EXE to LIBCURL

PostPosted: Sun May 23, 2021 7:14 am
by cnavarro
Christiano, try with this

Code: Select all  Expand view

local aHeaders  := {}
...
      AAdd( aHeaders, "accept: application/json" )
      AAdd( aHeaders, "Content-Type: application/json" )
      curl_easy_setopt( hCurl, HB_CURLOPT_HTTPHEADER, aHeaders )
...

 

Re: CURL.EXE to LIBCURL (SOLVED)

PostPosted: Wed May 26, 2021 5:09 pm
by ctoas
Thanks for the answer.

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

Re: CURL.EXE to LIBCURL

PostPosted: Sun May 30, 2021 8:20 am
by ssbbs
Hi! Can you translation the curl.exe command?

Code: Select all  Expand view

        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
 

Re: CURL.EXE to LIBCURL (SOLVED)

PostPosted: Sun May 30, 2021 2:32 pm
by cnavarro
Try with this

Code: Select all  Expand view

#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

 

Re: CURL.EXE to LIBCURL (SOLVED)

PostPosted: Sun May 30, 2021 3:06 pm
by ssbbs
cnavarro wrote:Try with this

Code: Select all  Expand view

#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: Select all  Expand view

   curl_easy_setopt( hCurl, HB_CURLOPT_POSTFIELDS, cParam )
 


change to :

Code: Select all  Expand view

   curl_easy_setopt( hCurl, HB_CURLOPT_POSTFIELDS, "message=Hello" )
 


is correct!!
bu image file 'd:/fwh/fwhteam/samples/ejemplo.jpg' not to send!! :(

Re: CURL.EXE to LIBCURL (SOLVED)

PostPosted: Sun May 30, 2021 11:22 pm
by cnavarro
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

Re: CURL.EXE to LIBCURL (SOLVED)

PostPosted: Mon May 31, 2021 1:50 am
by ssbbs