Edit

Curl_SetOpt (curlhandle, option, value)

Definition: Sets options on the handle. See the official curl documentation for more information on the options. Options that take a stringlist should be given an indexed array parameter (with numeric indexes atarting at 0). Options that take a callback should be given the name of the callback handler as a string. All other options take a number or string parameter.

Currently supported options are: CURLOPT_WRITEDATA, CURLOPT_URL, CURLOPT_PORT, CURLOPT_PROXY, CURLOPT_USERPWD, CURLOPT_PROXYUSERPWD, CURLOPT_RANGE, CURLOPT_READDATA, CURLOPT_ERRORBUFFER, CURLOPT_WRITEFUNCTION, CURLOPT_READFUNCTION, CURLOPT_TIMEOUT, CURLOPT_INFILESIZE, CURLOPT_POSTFIELDS, CURLOPT_REFERER, CURLOPT_FTPPORT, CURLOPT_USERAGENT, CURLOPT_LOW_SPEED_LIMIT, CURLOPT_LOW_SPEED_TIME, CURLOPT_RESUME_FROM, CURLOPT_COOKIE, CURLOPT_HTTPHEADER, CURLOPT_HTTPPOST, CURLOPT_SSLCERT, CURLOPT_KEYPASSWD, CURLOPT_CRLF, CURLOPT_QUOTE, CURLOPT_HEADERDATA, CURLOPT_COOKIEFILE, CURLOPT_SSLVERSION, CURLOPT_TIMECONDITION, CURLOPT_TIMEVALUE, CURLOPT_CUSTOMREQUEST, CURLOPT_STDERR, CURLOPT_POSTQUOTE, CURLOPT_VERBOSE, CURLOPT_HEADER, CURLOPT_NOPROGRESS, CURLOPT_NOBODY, CURLOPT_FAILONERROR, CURLOPT_UPLOAD, CURLOPT_POST, CURLOPT_DIRLISTONLY, CURLOPT_APPEND, CURLOPT_NETRC, CURLOPT_FOLLOWLOCATION, CURLOPT_TRANSFERTEXT, CURLOPT_PUT, CURLOPT_AUTOREFERER, CURLOPT_PROXYPORT, CURLOPT_POSTFIELDSIZE, CURLOPT_HTTPPROXYTUNNEL, CURLOPT_INTERFACE, CURLOPT_KRBLEVEL, CURLOPT_SSL_VERIFYPEER, CURLOPT_CAINFO, CURLOPT_MAXREDIRS, CURLOPT_FILETIME, CURLOPT_TELNETOPTIONS, CURLOPT_MAXCONNECTS, CURLOPT_FRESH_CONNECT, CURLOPT_FORBID_REUSE, CURLOPT_RANDOM_FILE, CURLOPT_EGDSOCKET, CURLOPT_CONNECTTIMEOUT, CURLOPT_HEADERFUNCTION, CURLOPT_HTTPGET, CURLOPT_SSL_VERIFYHOST, CURLOPT_COOKIEJAR, CURLOPT_SSL_CIPHER_LIST, CURLOPT_HTTP_VERSION, CURLOPT_FTP_USE_EPSV, CURLOPT_SSLCERTTYPE, CURLOPT_SSLKEY, CURLOPT_SSLKEYTYPE, CURLOPT_SSLENGINE, CURLOPT_SSLENGINE_DEFAULT, CURLOPT_DNS_USE_GLOBAL_CACHE, CURLOPT_DNS_CACHE_TIMEOUT, CURLOPT_PREQUOTE, CURLOPT_COOKIESESSION, CURLOPT_CAPATH, CURLOPT_BUFFERSIZE, CURLOPT_NOSIGNAL, CURLOPT_SHARE, CURLOPT_PROXYTYPE, CURLOPT_ACCEPT_ENCODING, CURLOPT_HTTP200ALIASES, CURLOPT_UNRESTRICTED_AUTH, CURLOPT_HTTPAUTH, CURLOPT_FTP_CREATE_MISSING_DIRS, CURLOPT_PROXYAUTH, CURLOPT_FTP_RESPONSE_TIMEOUT, CURLOPT_SERVER_RESPONSE_TIMEOUT, CURLOPT_IPRESOLVE, CURLOPT_MAXFILESIZE, CURLOPT_NETRC_FILE, CURLOPT_TCP_NODELAY, CURLOPT_FTPSSLAUTH, CURLOPT_FTP_ACCOUNT, CURLOPT_COPYPOSTFIELDS.

As of v8.1.7, CURLOPT_MAIL_FROM, CURLOPT_MAIL_RCPT, CURLOPT_MAIL_AUTH, CURLOPT_USERNAME, and CURLOPT_PASSWORD are also supported.

Examples: 

let fd = file_open("picture.jpg")
let ch = curl_init();
curl_setopt(ch, CURLOPT_URL, "https://myserver:6710/REST/Acme.moneyworks/image/product=BA100")
//    datacentre with folder login requires dual-domain Auth headers
let headers = CreateArray()
let headers[0] = "Authorization: Basic " + base64encode("root:Datacentre:myrootpassword")
let headers[1] = "Authorization: Basic " + base64encode("Admin:Document:docpass")
let headers[2] = "Content-Type: image/jpeg"
curl_setopt(ch, CURLOPT_HTTPHEADER, headers)
curl_setopt(ch, CURLOPT_PUT, 1)
curl_setopt(ch, CURLOPT_READDATA, fd)    // simplest: let default callback read the file
let result = curl_exec(ch)
file_close(fd)
curl_close(ch)

Note on calbacks:  To use a callback function (with CURLOPT_READFUNCTION or CURLOPT_HEADERFUNCTION or CURLOPT_WRITEFUNCTION) for reading or writing data, you supply a string containing the name of the callback handler in your script. A header or write callback receives a string parameter containing some amount of data that curl has read from the server. You can return FALSE to cancel the operation. A read callback receives the number of bytes to produce as the first parameter and the second parameter is the file handle you provide to the CURLOPT_READDATA option.

Example callback functions:  

on MyHeaderCallback(aHeader) 
    let headers = headers + Replace(aHeader, "\r", "")    // convert \r\n to \n
    // optionally return false to abort
end


on MyWriteCallback(someData) 
    let retval = retval + someData
    // optionally return false to abort
end

on MyReadCallback(maxBytesToProvide, readDataFrom)
    // file_read automatically restricts count to not go beyond end of file
    // when at end of file, will return empty string, which return
    // value signals end of data to curl
    let s = file_read(readDataFrom, maxBytesToProvide)
    return s
end

Installing a read callback function:   Ths will generate data to PUT to the server

    curl_setopt(ch, CURLOPT_PUT, 1)
    curl_setopt(ch, CURLOPT_READDATA, fd)    // will be passed to our callback
    curl_setopt(ch, CURLOPT_READFUNCTION, "MyReadCallback")    // our callback reads file

Availability:  available within MWScript handlers.

See Also:

Base64Decode: String from a base64 encoding

Base64Encode: Base64 of a string

Curl_Close: Finish with a CURL session

Curl_Exec: Execute a CURL session

Curl_Init: Start a CURL session

File_Open: Open a file

URLEncode: Convert url unsafe characters in a string to "%xx”