httpClient
With DolphinDB's httpClient plugin, you can send HTTP requests and send emails via SMTP or SMTPS. The plugin uses the third-party cURL library to perform HTTP requests and send emails.
Before using the email-sending feature, make sure that the target mail server support SMTP or SMTPS and have the corresponding service port enabled. If the email service provider has not enabled the relevant services by default, you must first enable SMTP/SMTPS for the sending mailbox and obtain the password or authorization code required by the provider.
The plugin automatically selects the mail transfer protocol based on the port configured
in emailSmtpConfig: it uses SMTPS when the port is 465 and SMTP for all
other ports. SMTPS enables SSL/TLS encryption as soon as the connection is
established.
Installation
Required server version: DolphinDB 2.00.10 or higher, Shark
Supported OS: Windows x64, Linux x64, Linux ARM, and Linux ABI.
Installation Steps:
- Use
listRemotePluginsto check plugin information in the plugin repository.login("admin", "123456") listRemotePlugins() - Use
installPluginfor plugin installation.installPlugin("httpClient") - Use
loadPluginto load the plugin before using the plugin methods.loadPlugin("httpClient")
Method References
httpGet
Syntax
httpGet(url, [params], [timeout], [headers])
Details
Sends an HTTP GET request.
Returns a dictionary containing the following keys:
- responseCode: HTTP status code.
- headers: HTTP response header.
- text: HTTP response body.
- elapsed: HTTP request elapsed time.
Parameters
- url: A STRING scalar indicating the URL of the HTTP request.
- params: A STRING scalar or a dictionary with STRING keys and values,
indicating the parameters of the HTTP request. After specified,
params will be appended to the end of url. Suppose
url is specified as "http://www.dolphindb.com":
- If params is a string (e.g., "example"), the URL of the HTTP request would be "http://www.dolphindb.com?example";
- If params is a dictionary (e.g., with 2 key-value pairs "name"->"ddb" and "id"->"111"), the URL of the HTTP request would be "http://www.dolphindb.com?id=111&name=ddb".
- timeout: An integer indicating the timeoout duration in milliseconds.
- headers: A STRING scalar or a dictionary with STRING keys and values,
indicating the headers of the HTTP request.
- If headers is a dictionary (e.g., with 2 key-value pairs "groupName"->"dolphindb" and "groupId"->"11"), the two headers "groupId: 11" and "groupName: dolphindb" will be appended to the HTTP request;
- If headers is a string, it must have the pattern
<key>: <value>and will be appended as a header to the request.
- config: A dictionary with STRING keys indicating configuration items.
Examples
loadPlugin('/home/DolphinDBPlugin/httpClient/PluginHttpClient.txt');
param = dict(string,string);
header = dict(string,string);
param['name'] = 'example';
param['id'] = '111';
header['groupName'] = 'dolphindb';
header['groupId'] = '11';
//Please set up your own httpServer ex.(python -m SimpleHTTPServer 8900)
url = "localhost:8900";
res = httpGet(url,param,1000,header);
httpPost
Syntax
httpPost(url, [params], [timeout], [headers])
Details
Sends an HTTP POST request.
Returns a dictionary containing the same keys as httpGet.
Parameters
Most parameters of httpPost are the same as those of
httpGet; only the differing parameters are described
here.
- params: A STRING scalar or a dictionary with STRING keys and values, indicating the parameters of the HTTP request. After specified, params will be included in the HTTP message body.
Examples
loadPlugin('/home/DolphinDBPlugin/httpClient/PluginHttpClient.txt');
param=dict(string,string);
header=dict(string,string);
param['name']='example';
param['id']='111';
header['groupName']='dolphindb';
header['groupId']='11';
//Please set up your own httpServer ex.(python -m SimpleHTTPServer 8900)
url = "localhost:8900";
res = httpClient::httpPost(url,toStdJson(param),1000,header);
httpPut
Syntax
httpPut(url, [params], [timeout], [headers], [config])
Details
Sends an HTTP PUT request.
Returns a dictionary containing the same keys as httpGet.
Parameters
httpPut contains the same parameters as httpGet.
httpDelete
Syntax
httpDelete(url, [params], [timeout], [headers], [config])
Details
Sends an HTTP DELETE request.
Returns a dictionary containing the same keys as httpGet.
Parameters
httpDelete contains the same parameters as httpGet.
emailSmtpConfig
Syntax
emailSmtpConfig(domain,host,[port=25])
Details
Configures the mail server address and port for the specified email domain. When
you call sendEmail to send an email, the plugin looks up the
matching configuration for the sender's email domain and automatically selects
the mail transfer protocol based on the configured port. Calling this function
only saves or updates the mail server configuration; it does not immediately
connect to the mail server or send email.
Parameters
- domain: A STRING scalar indicating the domain name of the email address, such as "gmail.com" and "yahoo.com".
- host: A STRING scalar indicating the SMTP server address.
- port: An integer indicating the port number of the email server. The default value is 25. The plugin automatically selects the mail transfer protocol based on this parameter: it uses SMTPS when the port is 465 and SMTP for all other ports. Therefore, if you omit this parameter, port 25 and the SMTP protocol are used.
Examples
domain = "qq.com";
host = "smtp.qq.com";
port = 25;
emailSmtpConfig(domain,host,[port=25]);
sendEmail
Syntax
sendEmail(userId,pwd,recipient,subject,body)
Details
Sends emails. Before sending an email, configure the mail server for the sender's
email domain through emailSmtpConfig.
sendEmail uses the server address and port in this
configuration, and automatically selects SMTP or SMTPS based on the port
number.
Returns a dictionary containing the following keys:
- userId: The sender's email account.
- recipient: A set of strings indicating the receivers' email accounts.
- responseCode: The response code returned by the HTTP request.
- headers: The headers returned by the HTTP request.
- text: The text returned by the HTTP request.
- elapsed: The time elapsed for the HTTP request.
Parameters
- userId: A STRING scalar indicating the sender’s email account.
- pwd: A STRING scalar indicating the sender’s email password.
- recipient: A STRING scalar or vector indicating the receivers' email accounts.
- subject: A STRING scalar indicating the email theme.
- body: A STRING scalar indicating the email body.
Examples
Example 1: Send an email.
sendEmail('MailFrom@xxx.com','xxxxx','Maildestination@xxx.com','This is a subject','It is a text');
recipient = 'Maildestination@xxx.com''Maildestination2@xxx.com''Maildestination3@xxx.com';
res = sendEmail('MailFrom@xxx.com','xxxxx',recipient,'This is a subject','It is a text');
httpClient::emailSmtpConfig(
"example.com",
"smtp.example.com",
465
)
userId = "sender@example.com"
pwd = "authorization-code"
recipient = "recipient@example.net"
subject = "SMTPS test"
body = "This message is sent over SMTPS."
res = httpClient::sendEmail(
userId,
pwd,
recipient,
subject,
body
)