-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathodoo_connect.cpp
More file actions
59 lines (50 loc) · 2.1 KB
/
odoo_connect.cpp
File metadata and controls
59 lines (50 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Compile: g++ -std=c++17 odoo_connect.cpp -o odoo_connect -lcurl
#include <iostream>
#include <string>
#include <curl/curl.h>
// Your Odoo Server Details
const std::string ODOO_URL = "http://YOUR_SERVER_IP:Port/xmlrpc/2/common";
const std::string DB_NAME = "odoo_db"; // Replace with actual DB
const std::string USERNAME = "user";
const std::string PASSWORD = "YOUR_API_KEY_OR_PASS";
// Helper for libcurl to write response to string
size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
int main() {
CURL* curl;
CURLcode res;
std::string readBuffer;
// Construct XML-RPC 'authenticate' payload manually
std::string xml_payload =
"<?xml version='1.0'?>"
"<methodCall>"
" <methodName>authenticate</methodName>"
" <params>"
" <param><value><string>" + DB_NAME + "</string></value></param>"
" <param><value><string>" + USERNAME + "</string></value></param>"
" <param><value><string>" + PASSWORD + "</string></value></param>"
" <param><value><array><data></data></array></value></param>"
" </params>"
"</methodCall>";
curl = curl_easy_init();
if(curl) {
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: text/xml");
curl_easy_setopt(curl, CURLOPT_URL, ODOO_URL.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, xml_payload.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
else
std::cout << "Response from Odoo:\n" << readBuffer << std::endl;
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
}
return 0;
}