Libcurl example to POST a Form does not work, need help

I need to use libcurl to send next CURL script (computer with Windows 10):

curl -X POST \
httpslink \
-H accept: */* \
-H Api-key: 5bedec80-079a-491d-9b1b-96c173ff24031 \
-H Content-Type:multipart/form-data \
-F RequestId=20250310-001 \
-F File= D:\path\to\testfile.xml;type=text/xml

I am new to this field. Can someone help me to proper convert this script to Clarion libcurl.
I have read messages from Mike Duglas and other, but did not succeeded.
According to examples I have converted my script to next:

!-----begin-------------------------
curl.Init()
curl.SetCustomRequest(‘POST’).
!-- headers
curl.AddHttpHeader('Accept: */*')
curl.AddHttpHeader('Content-Type: multipart/form-data')
curl.AddHttpHeader(' Api-key: 5bedec80-079a-491d-9b1b-96c173ff24031')
!-- applies http headers added by AddHttpHeader calls
curl.SetHttpHeaders()
!-- add form fields
cfres = curl.FormAdd('RequestId', '20250310-001')
cfres = curl.FormAdd('filename', 'testfile.xml')
!-- add file
cfres = curl.FormAddFile('File', ' D:\path\to')
respfile = 'FormPost_response.txt'
res = curl.FormPost(' httpslink', respfile)
!-----end-------------------------

When I try to run this example I always get message that it is failed to read file from local disk.
Where do I make errors ?
Please someone to help me.
Thank you in advance.
Best Regards,
Simo.

I noticed a few leading spaces (e.g. ' Api-key: ...') on the strings you are passing. Not sure if its a problem, but I would remove them:

curl.AddHttpHeader(' Api-key: 5bedec80-079a-491d-9b1b-96c173ff24031')
cfres = curl.FormAddFile('File', ' D:\path\to')
res = curl.FormPost(' httpslink', respfile)

You should also trying sending the result codes returned by the calls to Debug as they may give you a failure reason e.g. res = curl.FormPost(...) debug out the value in ‘res’.

Thank you Carl !
I will review code for leading spaces.
Did I converted other parts of Curl script in proper way to Clarion libcurl ?
Best regards,
Simo.

I don’t see “filename” argument in your source curl script.
Also, FormPost and related methods are considered as depricated, use mime post approach instead.

Using curl/libcurl is never easy the first time or the 2nd time.

I have implemented a mime file upload like this:

curl        tCurlHttpClass
mime        tCurlMimeClass
part        curl_mimepart
         CODE
          curl.SetUserPwd(usrname, passwrd)
          curl.SetCustomRequest('POST')
          curl.AddHttpHeader('Authorization: Basic ' & b64.GetValue())
          curl.AddHttpHeader(Choose(isBulk, 'X-Kofile-Bulk-Load: true', 'X-Kofile-Correlation-Id: ' & uuid))
          curl.AddHttpHeader('Content-Type: application/json')
          curl.SetHttpHeaders()
          curl.SetSSLVerifyPeer(FALSE)
          curl.SetSSLVerifyHost(FALSE)
          curl.UseSSL(CURLUSESSL_ALL)
 
             mime.Init(curl)
 
             part = mime.AddPart()
             mime.SetName(part, 'datafile')
             mime.SetData(part, fN.GetValue()) ! Fully qualifed file name
             mime.SetFileName(part, fN.FileNameOnly())
             mime.SetFileData(part, fN.GetValue())
             mime.SetType(part, fg:MimeType)     !'image/tif')
 
             curl.SetMimePost(mime)
 
             rtnStr = ''
             err = curl.SendRequestStr(url_endPt[2], , rtnStr)

This bit of code uploads a tiff file to a server. I may be missing something, but it does work. You will need to change the headers to match what your server is expecting. But this should get you going in the right direction.

Thank you Mike !
Reading messages here and documentation in github for libcurl somewhere I found examples with ‘filename’, so I used it.
I will try mime post approach.
Best Regards.
Simo.

Thank you John !
This is valuable code example for me on how to use mime approach.
I do not need to upload file, I must send XML file content along with RequestId as form fields.
I do not have problem with header part but with how to prepare form data fields, where in form I must have RequestId and content of testfile.xml.
Best regards,
Simo.

This is valuable code example for me on how to use mime approach

Glad it can help. It was cobbled together by reading every post on the news group with Libcurl in the title, studying the examples and the libcurl source, asked a couple of questions and then a lot of trial and error. What is really helpful is to have dbgview running. Libcurl posts everything it can to dbgview while it is executing. You will find a lot of valuable information and error codes in the output.