Libcurl faq (https://github.com/mikeduglas/libcurl)


#1

Many questions “how to translate CURL script to Clarion?” For example,

curl -X POST https://server.com/v1/person -H “Content-Type: application/json” -H “Authorization: Bearer Access-Token” -d “k1=v1&k2=v2”

-X POST means “POST” request and can be translated to curl.SetCustomRequest(‘POST’).
-H “…” means http header and can be translated to curl.AddHttpHeader(’…’).
To apply headers call curl.SetHttpHeaders().
-d “…” means post parameters which you pass to curl.SendRequest().

Sample code looks like this:

curl                            TCurlHttpClass
res                             CURLcode
url                             STRING(256), AUTO
PostParams                      STRING(4096), AUTO
respfile                        STRING(256), AUTO
  CODE
  curl.Init()

  !-X POST
  curl.SetCustomRequest('POST')
  
  !-H "Content-Type: application/json"
  curl.AddHttpHeader('Content-Type: application/json')

  !-H "Authorization: Bearer Access-Token"
  curl.AddHttpHeader('Authorization: Bearer Access-Token')

  !-- applies http headers added by AddHttpHeader calls
  curl.SetHttpHeaders()
  
  url = 'https://server.com/v1/person'
  PostParams = 'k1=v1&k2=v2'
  respfile = 'response.txt'
  
  res = curl.SendRequest(url, PostParams, respfile)
  IF res = CURLE_OK
    MESSAGE('Success, see file '& CLIP(respfile) &' for details', 'Post test', ICON:Asterisk)
  ELSIF res = -1
    MESSAGE('SendRequest failed: errcode '& res, 'Post test', ICON:Asterisk)
  ELSE
    MESSAGE('SendRequest failed: '& curl.StrError(res), 'libcurl', ICON:Exclamation)
  END

#2

Another example:

curl -u jsmith:jsmith -X POST -F “environment=TEM” -F “filename=Test” -F “[email protected]” ‘https://server.com/api/manifests

-u jsmith:jsmith defines login and password and translated to curl.SetUserPwd(‘jsmith’, ‘jsmith’).
-F “environment=TEM” sends the data as a multipart formpost: curl.FormAdd(‘environment’, ‘TEM’).
-F “[email protected]” sends a file: curl.FormAddFile(‘multipartFile’, ‘mmabc.manifest’).
For formpost, call curl.FormPost() method instead of curl.SendRequest():

  curl.Init()

  !-- add form fields
  cfres = curl.FormAdd('environment', 'TEM')
  cfres = curl.FormAdd('filename', 'Test')
  !-- add file
  cfres = curl.FormAddFile('multipartFile', 'mmabc.manifest')

  curl.SetUserPwd('jsmith', 'jsmith')

  respfile = 'FormPost_response.txt'
  res = curl.FormPost('https://server.com/api/manifests', respfile)

#3

Send a file over http:

curl http://server.com -H “Content-Type: application/json” --data-binary @test.json

Call curl.SendFile() with appropriate mime type (“application/json” in this case):

  curl.Init()

  url = 'http://server.com'
  infile = 'test.json'
  respfile = 'send_response.json'
  
  res = curl.SendFile(url, infile, 'application/json', respfile)

#4

To test a connection:

curl.SetOpt(CURLOPT_CONNECT_ONLY, TRUE)
res = curl.SendRequest(url)
IF res = CURLE_OK
  !- success
ELSE
  !- fail
END

#5

Q. I’m trying to send email, no errors reported but the email never arrives at the destination.
A. If server url starts with email protocol (“smtp.gmail.com”), libcurl uses this protocol (smtp) to send a request; otherwise it uses http by default, so you must change it by calling curl.SetDefaultProtocol(‘smtp’).

UPD:
since v1.38 TCurlFtpClass uses ‘ftp’ default protocol; TCurlMailClass uses ‘smtp’ default protocol, so calling SetDefaultProtocol is not required.