Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

CURL.SetOptionCookieFile

Sets the cookie file location.

Component Version macOS Windows Linux Server iOS SDK
CURL 2.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "CURL.SetOptionCookieFile"; curl; Value { ; Encoding } )   More

Parameters

Parameter Description Example Flags
curl The CURL session handle. $curl
Value The file path to the cookie file. "/tmp/cookies.txt"
Encoding The text encoding for text parameter.
Default is UTF-8.
Possible encoding names: ANSI, ISO-8859-1, Latin1, Mac, Native, UTF-8, DOS, Hex, Base64 or Windows. More listed in the FAQ.
"UTF8" Optional

Result

Returns "OK" on success.

Description

Sets the cookie file location.
Pass a file path for the cookie file. It should contain the name of your file holding cookie data to read. The cookie data may be in Netscape / Mozilla cookie data format or just regular HTTP-style headers dumped to a file.

Given an empty or non-existing file or by passing the empty string (""), this option will enable cookies for this curl handle, making it understand and parse received cookies and then use matching cookies in future requests.

If you use this option multiple times, you just add more files to read. Subsequent files will add more cookies.

Starting with version 8.0 the plugin will always use UTF-8 encoding for file path on Linux and macOS. For macOS we also do the unicode character normalization for file names for you.

See also COOKIEFILE option in CURL manual.

Examples

Activate cookie engine without cookie file:

MBS( "CURL.SetOptionCookieFile"; $curl; "")

Perform SOAP service with login and use cookies in second request:

# The URL of the service
Set Variable [ $url ; Value: "http://somedomain.com/PartnerService17.asmx" ]
# Build XML for SOAP Login request, e.g. with MBS XML functions or simply subsitute()
Set Variable [ $xml ; Value: "<?xml version=\"1.0\" encoding=\"utf-8\"?> <soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> <soap:Body> </soap:Body> …" ]
Set Field [ Curl_Test::XML ; $xml ]
# The SOAP action to use
Set Variable [ $action ; Value: "http://somedomain.com/partner/loginWithLicenceUnlockInfo" ]
#
# Setup transfer
Set Variable [ $curl ; Value: MBS("CURL.New") ]
Set Variable [ $result ; Value: MBS("CURL.SetOptionURL"; $curl; $url) ]
# Put SOAP action and XML data type in the headers
Set Variable [ $result ; Value: MBS("CURL.SetOptionHTTPHeader"; $curl; "Content-Type: text/xml; charset=utf-8"; "Expect:"; "SOAPAction: \""& $action & "\"") ]
# Ask CURL to manage cookies
Set Variable [ $result ; Value: MBS("CURL.SetOptionCookieFile"; $curl; "" ) ]
# Set the payload for the login transfer
Set Variable [ $result ; Value: MBS("CURL.SetOptionPostFields"; $curl; $xml) ]
# Run transfer
Set Variable [ $result ; Value: MBS("CURL.Perform"; $curl) ]
# Let's take a look on the cookie list
Set Variable [ $$cookie ; Value: MBS("CURL.GetCookieList"; $curl ) ]
Set Field [ Curl_Test::Cookie ; $$cookie ]
# Check result
Set Field [ Curl_Test::Debug ; MBS("CURL.GetDebugMessages"; $curl) ]
Set Variable [ $result ; Value: MBS("CURL.GetResultAsText"; $curl) ]
Set Field [ Curl_Test::Resultat ; $result ]
#
# Reuse same $curl for second request reusing existing cookies
#
# Make new XML for second request
Set Variable [ $npk ; Value: "103" ]
# Build XML for SOAP Login request, e.g. with MBS XML functions or simply subsitute()
Set Variable [ $xml ; Value: "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> <soap:Body> </soap:Body> </soap:Envelope>" ]
Set Field [ Curl_Test::XML ; $xml ]
Set Variable [ $action ; Value: "http://somedomain.com/partner/queryInfo" ]
#
# Setup transfer
Set Variable [ $result ; Value: MBS("CURL.SetOptionURL"; $curl; $url) ]
# Put SOAP action and XML data type in the headers
Set Variable [ $result ; Value: MBS("CURL.SetOptionHTTPHeader"; $curl; "Content-Type: text/xml; charset=utf-8"; "Expect:"; "SOAPAction: \""& $action & "\"") ]
# Set the payload for the second transfer
Set Variable [ $result ; Value: MBS("CURL.SetOptionPostFields"; $curl; $xml) ]
# Run transfer
Set Variable [ $result ; Value: MBS("CURL.Perform"; $curl) ]
# Check result
Set Field [ Curl_Test::Debug ; MBS("CURL.GetDebugMessages"; $curl) ]
Set Variable [ $result ; Value: MBS("CURL.GetResultAsText"; $curl) ]
Set Field [ Curl_Test::Resultat ; $result ]
# Finally end curl session
Set Variable [ $r ; Value: MBS("CURL.Release"; $curl) ]

See also

Release notes

Blog Entries

Created 18th August 2014, last changed 19th April 2020


CURL.SetOptionCookie - CURL.SetOptionCookieJar