How can I use CURL to consume an API?

Hi everyone,

I’m new to consuming APIs from a Clarion application and I’m trying to make a POST request like the following:

curl -X POST “https://api.valida-cfdi.com.mx/v1/zapier/actions/validate-cfdi
-H “X-API-Key: vck_tu_api_key”
-H “Content-Type: application/json”
-d ‘{
“uuid”: “12345678-1234-1234-1234-123456789012”,
“rfc_emisor”: “ABC123456XYZ”,
“rfc_receptor”: “DEF789012UVW”,
“total”: 1500.00
}’
What would be the best way to implement this in Clarion?

Does anyone have an example of making a POST request with custom headers and a JSON payload from a Clarion application?

Any advice, sample code, or recommended libraries would be greatly appreciated.

Thanks in advance!

Regards,

Go here…

…download the apps and see how to do it. There are several examples, including using APIs.

Ask more questions and provide more details, if necessary.

LibCurl is a good option.

jFiles will help with creating JSON payloads.
https://capesoft.com/accessories/jFilessp.htm

NetTalk Desktop is an alternative to LibCurl.
Using this allows you to use CurlCode which translates Curl statements into NetTalk for you.

https://capesoft.com/curlcode

https://capesoft.com/accessories/netsp.htm

As has been suggested, get libcurl and follow the install instructions. You can use jFiles3 from CapeSoft, cJSON from Mike Duglas or use Clarions jFiles implementation.

You will also possibly need something that can handle large strings. Either StringTheory from Capesoft, System String or IDynStr both in Clarion.

To actually use it in a Clarion program, I’ve used the following:

curl      tcurlHTTPClass
rtnStr   &IDynStr

CODE

  !Create your json with the tool of your choice

   rtnStr &= NewDynStr()
 
    curl.Init()
    curl.SetCustomRequest('POST')
    curl.AddHttpHeader('X-API-Key: vck_tu_api_key')
    curl.AddHttpHeader('Content-Type:application/json')
    curl.SetHttpHeaders()
    curl.SetOpt(CURLOPT_FOLLOWLOCATION, 1)
    curl.SetSSLVerifyPeer(FALSE)
    curl.SetSSLVerifyHost(FALSE)
    curl.UseSSL(CURLUSESSL_ALL)
 
    curl.SendRequest('https://api.valida-cfdi.com.mx/v1/zapier/actions/validate-cfdi',  'your json data' ,  rtnStr)
 
   if curl.GetResponseCode() = 200   !Success
        someOtherString = rntStr.Str()
   else
      !Process error in rtnStr.Str()
   end

This is obviously not in any way a complete example. Just something to show the basic steps to get json to upload using libcurl.

Where would one find this John?

jFiles is a capesoft product

Clarions implementation of json is in \ClarionXX\LibSrc\win . There you can find json.inc and json.clw. I use jFiles so much so that is what my fingers typed out. The point is that Clarion has a json solution, albeit not as well known as jFiles, at no extra cost.