cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 
2024 Technology Preview Program

2024 Technology Preview Program:
Master powerful new features and shape the latest BIM-enabled innovations

Archicad C++ API
About Archicad add-on development using the C++ API.

Why can't Add-On be used when I use curl?

luna
Contributor

I am developing with AC24 and VS2017.

 

I want to use curl to access the Http interface and use JsonCpp to parse the data, but every time #include <curl/curl.h>, the Add-On doesn't work.

When I remove the #include <curl/curl.h> code, the plugin works fine.

 

Is this a version problem or some other reason, please give me some tips.

 

addonquestion.png

 

 

3 REPLIES 3
luna
Contributor


The reason I found some details was the Get and Post functions, and when I added these two methods to the file, AddOn didn't work.

 

CURLcode HttpUtils::PostUrl(const string &url, string& response, const string&token, const string&postParams) {
	CURL*m_curl = curl_easy_init();
	CURL* m_curl = curl_multi_init();
	
	if (m_curl) {
		struct curl_slist* head_list = NULL;
		head_list = curl_slist_append(head_list, "Content-Type:application/json; charset=UTF-8");
		if (!token.empty()) {
			string tokenstr = "token:" + token;
			head_list = curl_slist_append(head_list, tokenstr.c_str());
		}
		//tenant_id = "tenant-id" + const_;
		head_list = curl_slist_append(head_list, tenant_id.c_str());
		head_list = curl_slist_append(head_list, "Changhong-type:ARCHICAD");
		head_list = curl_slist_append(head_list, std::string("Authorization:Bearer " + token).c_str());
		curl_easy_setopt(m_curl, CURLOPT_HTTPHEADER, head_list);
		curl_easy_setopt(m_curl, CURLOPT_HEADER, 0);
		curl_easy_setopt(m_curl, CURLOPT_POST, 1);
		curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str());
		if (!postParams.empty()) {
			curl_easy_setopt(m_curl, CURLOPT_POSTFIELDS, postParams.c_str());
		}

		
		curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, req_reply);
		curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, (void *)&response);

		curl_easy_setopt(m_curl, CURLOPT_NOSIGNAL, 1);

		
		curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, 20);
		curl_easy_setopt(m_curl, CURLOPT_TIMEOUT, 20);

	
		m_rescode = curl_easy_perform(m_curl);

	}
	curl_easy_cleanup(m_curl);
	return m_rescode;
}
CURLcode HttpUtils::GetUrl(const string &url, string& response, const string&token) {
	 CURL*m_curl = curl_easy_init();
	if (m_curl) {
		struct curl_slist* head_list = NULL;
		head_list = curl_slist_append(head_list, "Content-Type:application/json;charset=UTF-8");
		if (!token.empty()) {
			string tokenstr = "token:" + token;
			head_list = curl_slist_append(head_list, tokenstr.c_str());
		}
		head_list = curl_slist_append(head_list, tenant_id.c_str());
		head_list = curl_slist_append(head_list, "Changhong-type:ARCHICAD");
		head_list = curl_slist_append(head_list, std::string("Authorization:Bearer " + token).c_str());
		//head_list = curl_slist_append(head_list, "token:"+);
		
		curl_easy_setopt(m_curl, CURLOPT_HTTPHEADER, head_list);
		curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str()); // url  
		curl_easy_setopt(m_curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https  
		curl_easy_setopt(m_curl, CURLOPT_SSL_VERIFYHOST, false); // set peer and host verify false  
		curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 0);
		curl_easy_setopt(m_curl, CURLOPT_READFUNCTION, NULL);
		curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, req_reply);
		curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, (void *)&response);
		curl_easy_setopt(m_curl, CURLOPT_NOSIGNAL, 1);
		curl_easy_setopt(m_curl, CURLOPT_HEADER, 0);
		curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, 3); // set transport and time out time  
		curl_easy_setopt(m_curl, CURLOPT_TIMEOUT, 3);

		curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, 6);
		curl_easy_setopt(m_curl, CURLOPT_TIMEOUT, 6);

		// open post request
		m_rescode = curl_easy_perform(m_curl);

	}
	curl_easy_cleanup(m_curl);

	return m_rescode;
}

 

🙂

luna
Contributor

More specifically, the problem is with the curl_easy_  method.

 

addonquestion2.png

ManelCG
Contributor

Hello,

 

The issue is most likely that curl is being linked dynamically. We also had this problem.

 

The most likely solution is to try to force Curl to be linked statically. That is, if on Windows, force it to compile as .lib and then link the .lib on it.

 

Generally, AC will give that error whenever it cannot find a dll, which will be almost every time when you link something dynamically.

 

I also recommend to give libcpr a try. It is a very, very easy to use and to include curl wrapper.

 

Good luck!