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
3 Likes
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)
2 Likes
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)
3 Likes
To test a connection:
curl.SetOpt(CURLOPT_CONNECT_ONLY, TRUE)
res = curl.SendRequest(url)
IF res = CURLE_OK
!- success
ELSE
!- fail
END
2 Likes
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.
1 Like
Q. How to translate following NetTalk code into libcurl?
webclient.SetValue('user', 'mike')
webclient.SetValue('password', '12345')
webclient.SetValue('file', FileName, true)
webclient.Post(url)
A. curl is TCurlHttpClass:
curl.FormAdd('user', 'mike')
curl.FormAdd('password', '12345')
curl.FormAddFile('file', filename)
res = curl.FormPost(url)
Hi!
Thanks for your work!!!
What about updating to current libcurl (7.65.3)?? It has a lot of bugfixes since v7.63.0…
TIA!!
Flavio,
v1.43 is available to download.
WOW!!!
This was FAST!!!
Thank you!!!
Q. How to add HTTP headers? I used
curl.AddHttpHeader(‘x-test: text’)
but those headers never make it to the PHP service.
A. Call curl.SetHttpHeaders() to commit headers added by AddHttpHeader():
curl.AddHttpHeader(‘x-test: text’)
curl.AddHttpHeader(‘y-test: text’)
curl.SetHttpHeaders()
1 Like
Q. a SSL site throws errors when trying to connect.
A1. Don’t verify the authenticity:
curl.SetSSLVerifyPeer(FALSE) !- Don't verify the authenticity of the peer's certificate.
curl.SetSSLVerifyHost(FALSE) !- the connection succeeds regardless of the names in the certificate.
A2. Download the Mozilla CA certificate store PEM file from here and call this:
curl.SetCAInfo('cacert.pem')
curl.SetSSLVerifyPeer(TRUE) !- not necessary, it is default option
curl.SetSSLVerifyHost(TRUE) !- not necessary, it is default option
Thanks to Arnor Baldvinsson for sharing.
1 Like
Hi,
What license is used for this code?
Regards,
Jared
No license is used for this code.
Hi Mike,
I am stuck.
I have setup:
respBuffer &IDynStr
And I use: res = curl.SendRequest(Loc:URL, PostString, respBuffer)
The result I get back is big and it seems to be cutoff before the end of the message.
The start tag I need to get the data from is there but there is no end tag.
The return is a XML file and the section I need is Base64 encoded.
Regards
Johan de Klerk
Johan,
What happens if you call SendRequest to save response to a file?
Hi Mike,
I then get the complete return with the end tags.
Regards
Johan de Klerk
Well, then check dynstr buffer - save its content to a file.
Hi Mike,
Thanks for the suggestion.
I can now save the complete file.
Load it into StringTheory, extract the section I need, Base64Decode it, save it as a ZIP file.
The only problem that I still need to solve is how to get the files extracted from the saved ZIP file.
Regards
Johan de Klerk
Hi Johan,
I just suggested to check dynstr buffer validity.
Hi Mike,
Thanks all sorted.
Works perfect.
LibCurl to the rescue again.
Regards
Johan de Klerk