CURL.EXE to LIBCURL (SOLVED)

CURL.EXE to LIBCURL (SOLVED)

Postby ctoas » Thu May 20, 2021 11:11 pm

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
Last edited by ctoas on Wed May 26, 2021 5:09 pm, edited 1 time in total.
Christiano Augusto Silveira
christiano.silveira@gmail.com

MaxxTech Soluções em TI
http://www.maxxtech.com.br
User avatar
ctoas
 
Posts: 115
Joined: Wed Oct 26, 2005 2:38 pm
Location: São Paulo - Brasil

Re: CURL.EXE to LIBCURL

Postby cnavarro » Sun May 23, 2021 7:14 am

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 )
...

 
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
User avatar
cnavarro
 
Posts: 6541
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: CURL.EXE to LIBCURL (SOLVED)

Postby ctoas » Wed May 26, 2021 5: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
User avatar
ctoas
 
Posts: 115
Joined: Wed Oct 26, 2005 2:38 pm
Location: São Paulo - Brasil

Re: CURL.EXE to LIBCURL

Postby ssbbs » Sun May 30, 2021 8:20 am

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
 
line ID: ssbbstw
WeChat ID: ssbbstw
User avatar
ssbbs
 
Posts: 104
Joined: Mon Oct 17, 2005 3:03 am
Location: Taiwan

Re: CURL.EXE to LIBCURL (SOLVED)

Postby cnavarro » Sun May 30, 2021 2:32 pm

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

 
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
User avatar
cnavarro
 
Posts: 6541
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: CURL.EXE to LIBCURL (SOLVED)

Postby ssbbs » Sun May 30, 2021 3:06 pm

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!! :(
line ID: ssbbstw
WeChat ID: ssbbstw
User avatar
ssbbs
 
Posts: 104
Joined: Mon Oct 17, 2005 3:03 am
Location: Taiwan

Re: CURL.EXE to LIBCURL (SOLVED)

Postby cnavarro » 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
User avatar
cnavarro
 
Posts: 6541
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: CURL.EXE to LIBCURL (SOLVED)

Postby ssbbs » Mon May 31, 2021 1:50 am

line ID: ssbbstw
WeChat ID: ssbbstw
User avatar
ssbbs
 
Posts: 104
Joined: Mon Oct 17, 2005 3:03 am
Location: Taiwan


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 112 guests