How to POST multidimensional array data with cURL include file

544 views
Skip to first unread message

GM

unread,
Jul 8, 2019, 6:11:29 AM7/8/19
to Harbour Users
Hi guys,

I'm trying to post an array with cURL.
I don't have problem to send a simple array, but i have when arrays include the file.
I would like to send some parameters and one pdf in multidimensional array, but not send the file, just the filename and the other paramters.
How can i solve this problem?

Something like this: 


   fpParams:={}

 

   
AAdd(fpParams,{"emailfrom", "ma...@mail.com"})

   
AAdd(fpParams,{"sender", "John"})

   
AAdd(fpParams,{"id", "123"})

   
AAdd(fpParams,{"attach", "test.pdf"})   // ???

   
Curl_Func(fpParams, .T.)



 

   FUNCTION Curl_Func(params, postRequest, resultFile)

 

 

   _cURL
:="http://localhost/test.php"

 

// Default

    IF
(params == NIL); params := {}; ENDIF

    IF
(postRequest == NIL); postRequest := .F.; ENDIF

 

   
// Init cURL

    curl_global_init
()

    curl
:= curl_easy_init()

 

   
// Do NOT include header in output

    curl_easy_setopt
(curl, HB_CURLOPT_HEADER, .F.)

 

   
// Parameters

    FOR EACH param IN
params

        requestParams
+= param[1] + "=" + curl_easy_escape(curl, AllTrim(HB_ValToStr(param[2]))) + "&"

    NEXT

 

   
// Removes trailing &

    IF
(LEN(requestParams) > 0)

        requestParams
:= LEFT(requestParams, LEN(requestParams) - 1)

    ENDIF

 

    IF
(postRequest)

       
// POST request

        curl_easy_setopt
(curl, HB_CURLOPT_NOSIGNAL, 1)

        curl_easy_setopt
(curl, HB_CURLOPT_TIMEOUT_MS, 2000)

        curl_easy_setopt
(curl, HB_CURLOPT_POST, .T.)

        curl_easy_setopt
(curl, HB_CURLOPT_POSTFIELDS, requestParams)

        curl_easy_setopt
(curl, HB_CURLOPT_URL, _cURL)

    ELSE

          curl_easy_setopt
(curl, HB_CURLOPT_URL, _cURL + "?" + requestParams)

    ENDIF

 

   
// Save result to file?

    IF
(resultFile != NIL)

        curl_easy_setopt
(curl, HB_CURLOPT_DL_FILE_SETUP, resultFile)

        result
:= .T.

    ELSE

        curl_easy_setopt
(curl, HB_CURLOPT_DL_BUFF_SETUP)

    ENDIF

   
// Execute

    curl_easy_perform
(curl)

 

   
// Load result from buffer into variable

    IF
(result == NIL)

        result
:= curl_easy_dl_buff_get(curl)

    ENDIF

 

    curl_easy_cleanup
(curl)

    curl_global_cleanup
()

 

RETURN result




GM

unread,
Jul 18, 2019, 6:22:54 AM7/18/19
to Harbour Users
It works in curl command line, but i don't necessarily want to use the curl.exe. Better solution is the lib or OLE.

Function cURLexe()


Local cPostUrl        := 'http://localhost/test.php'

Local cEXE               := 'curl.exe '
Local cMode           := "-X POST " + cPostURL +''

Local cData             := ''

Local cSilent           := '-s '

Local cCmd      

Local cOut              := '-o output.txt '

Local cEmailfrom  := 'ma...@mail.com'

Local cSender        := 'John'

Local cId                 := '123'



cData :=   '-H "Content-Type:multipart/form-data" \  '+;

                  '-H "cache-control:no-cache" \  '+;

                  '-H "content-type: multipart/form-data" \ '+;

                  '-F emailfrom='+cEmailfrom+'\  '+;

                  '-F sender='+cSender+'\  '+;

                  '-F id='+cId+'\  '+;

                  '-F attach=@test.pdf  '


cCmd := cEXE     + cMode  + cData + cSilent + cOut


return hb_run(cCmd)


or something similar without curl. How can i add test.pdf.

formdata:='emailfrom=mail@mail.com&sender=John&id=123&'
attach
="test.pdf"


oHttp
:=createobject('msxml2.xmlhttp.6.0')

oHttp:Open( 'POST', "http://localhost/test.php", .F.)

ohttp:SetRequestHeader("Content-Type" ,  "application/x-www-form-urlencoded")   // if just send string is ok

// ohttp:SetRequestHeader("Content-Type" ,  "multipart/form-data" )       //if i would like to send file, but i don't know how

oHttp:Send(formdata)







 

Alexandre Cavalcante Alencar

unread,
Jul 20, 2019, 4:28:04 PM7/20/19
to Harbour Users
If you are using hbcurl, you can simple use what is libcurl docs, just prefixing constants with HB_.

...
curl_global_init()
...
If ! Empty( curl := curl_easy_init() )
    ...
    ? curl_easy_setopt( curl, HB_CURLOPT_UPLOAD )
    ? curl_easy_setopt( curl, HB_CURLOPT_URL, "<proto>://<host>/<uri>/" )
    ? curl_easy_setopt( curl, HB_CURLOPT_UL_FILE_SETUP, cFileToUpload )
    ? curl_easy_setopt( curl, HB_CURLOPT_VERBOSE, .T. )
    result := curl_easy_perform( curl )
    If( res != HB_CURL_OK )
        ? Check for errors
    EndIf

GM

unread,
Jul 23, 2019, 8:13:23 AM7/23/19
to Harbour Users
Hi Alexandre,

Thanks for your answer.
I know that you wrote and i've used hbcurl several times, but my problem is that to send multidimensional data and 1 file with cURL in one request.
In a way similar to this. (link can be found in my first post) 

Don Lowenstein

unread,
Jul 25, 2019, 12:34:32 PM7/25/19
to harbou...@googlegroups.com

The HBPersistent class allows you to convert any array to text.

After you send the text, the server can convert it back to a Harbour array.

--
--
You received this message because you are subscribed to the Google
Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: http://groups.google.com/group/harbour-users

---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/harbour-users/7bf761e0-5de9-41d6-b131-4c01c7f404d5%40googlegroups.com.

GM

unread,
Jul 26, 2019, 4:30:43 AM7/26/19
to Harbour Users
Thank you Don for the idea. 
I will check.

Diego Pego

unread,
Feb 7, 2020, 6:44:04 PM2/7/20
to Harbour Users
hb_jsonencode and hb_jsondecode are better suited IMHO
Reply all
Reply to author
Forward
0 new messages