FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour libcurl issues
Posts: 3022
Joined: Fri Oct 07, 2005 01:45 PM
libcurl issues
Posted: Fri Feb 10, 2023 10:48 PM
I have successfully used libcurl for some processes, but for a new task, I have uncovered two issues:

curl_easy_setopt(hCurl, HB_CURLOPT_FOLLOWLOCATION, 1L). When trying to compile this, Harbour errors at the L ( in 1L ). This is a standard call.

curl_easy_setopt(hCurl, HB_CURLOPT_DEFAULT_PROTOCOL, "https"). This line errors because the hbcurl.ch does not have a listing for DEFAULT_PROTOCOL

These are two standard libcurl settings. My include file is from the latest version of Harbour supplied by FiveTech.

Any assistance would be appreciated.

I do have the right libraries linked in. I also have curl running smoothly on other tasks.

Thank you.
Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
Posts: 1772
Joined: Thu Sep 05, 2019 05:32 AM
Re: libcurl issues
Posted: Sat Feb 11, 2023 05:02 AM
hi Tim,
TimStone wrote: curl_easy_setopt(hCurl, HB_CURLOPT_DEFAULT_PROTOCOL, "https").
there is not HB_CURLOPT_DEFAULT_PROTOCOL but HB_CURLOPT_PROTOCOLS
you can use it with HB_CURLPROTO_HTTPS or HB_CURLPROTO_HTTP
greeting,

Jimmy
Posts: 3022
Joined: Fri Oct 07, 2005 01:45 PM
Re: libcurl issues
Posted: Sat Feb 11, 2023 05:14 AM

Thank you

Sent from my iPhone using Tapatalk

Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
Posts: 3022
Joined: Fri Oct 07, 2005 01:45 PM
Re: libcurl issues
Posted: Sat Feb 11, 2023 07:16 AM
Sometimes no matter how long you look at something, the answer just doesn't come, even when trying multiple approaches. This is one of them.

Still using libcurl:

This is a working format in straight c-libcurl:
Code (fw): Select all Collapse
  const char *data = "{\r\n  \"transactionData\": {\r\n    \"transactionType\": \"CreditSale\",\r\n    \"method\": \"hostedFields\",\r\n    \"submissionType\": \"automatic\",\r\n    \"fields\": [\r\n      {\r\n        \"id\": \"base_amount\",\r\n        \"value\": \"1.00\"\r\n      },\r\n      {\r\n        \"id\": \"external_tran_id\",\r\n        \"value\": \"ae898295-c8b8-4799-b2c1-88e5067be721\"\r\n      },\r\n      {\r\n         \"id\": \"device_name\",\r\n        \"value\": \"Flex-0554\"\r\n      }\r\n    ]\r\n  }\r\n}";
Here is the same code in PHP-CURL:
Code (fw): Select all Collapse
  CURLOPT_POSTFIELDS =>'{
  "transactionData": {
    "transactionType": "CreditSale",
    "method": "hostedFields",
    "submissionType": "automatic",
    "fields": [
      {
        "id": "base_amount",
        "value": "1.00"
      },
      {
        "id": "external_tran_id",
        "value": ""
      },
      {
         "id": "device_name",
        "value": "Flex-0554"
      }
    ]
  }
}',
and in straight cURL:
Code (fw): Select all Collapse
--data-raw '{
  "transactionData": {
    "transactionType": "CreditSale",
    "method": "hostedFields",
    "submissionType": "automatic",
    "fields": [
      {
        "id": "base_amount",
        "value": "1.00"
      },
      {
        "id": "external_tran_id",
        "value": ""
      },
      {
         "id": "device_name",
        "value": "Flex-0554"
      }
    ]
  }
}'
These are values for cPostFields to pass with curl_easy_setopt(hCurl, HB_CURLOPT_POSTFIELDS, cPostFields).

I've viewed a lot of samples, and tried approaches, but I cannot find a "Harbour friendly" format to write this string. Anyone confident on how to approach this ?

Thanks.
Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
Posts: 1344
Joined: Wed Nov 16, 2005 09:14 PM
Re: libcurl issues
Posted: Sat Feb 11, 2023 10:01 AM
Tim, no se si esto es lo que buscas, como generar ese json
Code (fw): Select all Collapse
#include "fivewin.ch"
function main() 
LOCAL aData, aField, aRecord, aFields1 := {}, cJson
aData   := hash()
aRecord := hash()

aRecord["transactionType"] = "CreditSale"
aRecord["method"]          = "hostedFields"
aRecord["submissionType"]  = "automatic"

aField  := hash()
aField["id"]    =  "base_amount"
aField["value"] =  "1.00"
AADD(aFields1,aField)
aField  := hash()
aField["id"]    =  "external_tran_id"
aField["value"] =  ""
AADD(aFields1,aField)
aField  := hash()
aField["id"]    =  "device_name"
aField["value"] =  "Flex-0554"
AADD(aFields1,aField)
aRecord["fields"] = aFields1

aData["transactionData"] = aRecord
cJson := hb_jsonEncode(aData,.f.)
memowrit("json.txt",cJson)
return nil
Mira el archivo json.txt para ver el resultado si es el esperado
Espero te sea de utilidad
Posts: 3022
Joined: Fri Oct 07, 2005 01:45 PM
Re: libcurl issues
Posted: Sat Feb 11, 2023 05:09 PM

I don't know that I have to encode the string as json. I have done that for other external systems where I'm passing data.

The examples I have provided were all being sent directly, as shown, to the website using "Postman". They work correctly. However, they are all variants from the Harbour code ( or FWH samples I see ) using libcurl.

Any other thoughts ?

Yes ... I can test this to see if it works but I think it's really just how I format the data similar to how the examples show, but that Harbour can actually use.

Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: libcurl issues
Posted: Sun Feb 12, 2023 03:47 AM
Try with
Code (fw): Select all Collapse
   Local cRequest

   TEXT INTO cRequest
{
  "transactionData": {
    "transactionType": "CreditSale",
    "method": "hostedFields",
    "submissionType": "automatic",
    "fields": [
      {
        "id": "base_amount",
        "value": "1.00"
      },
      {
        "id": "external_tran_id",
        "value": ""
      },
      {
         "id": "device_name",
        "value": "Flex-0554"
      }
    ]
  }
}
   ENDTEXT

   ? cRequest
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

Continue the discussion