@cpp.js/package-curl-wasm-multithread
v2.0.0-beta.1
Published
This package provides a CURL library compiled with Cpp.js, enabling seamless usage of CURL functionalities in JavaScript, WebAssembly and React Native projects. Client-side URL transfers for web and mobile applications.
Downloads
9
Maintainers
Readme
@cpp.js/package-curl
Precompiled CURL library built with cpp.js for seamless integration in JavaScript, WebAssembly and React Native projects.
Integration
Start by installing these package with the following command:
npm install @cpp.js/package-curlTo enable the library, modify the cppjs.config.js file as shown below.
+import curl from '@cpp.js/package-curl/cppjs.config.js';
export default {
dependencies: [
+ curl
]
paths: {
config: import.meta.url,
}
};Usage
Below are the steps to use the curl in your C++ or JavaScript code.
Usage in C++ Code
+#include <curl/curl.h>
+size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* output) {
+ size_t totalSize = size * nmemb;
+ output->append((char*)contents, totalSize);
+ return totalSize;
+}
std::string Native::sample() {
+ std::string result = "";
+ std::string response;
+ char errbuf[CURL_ERROR_SIZE*100];
+
+ CURL* curl = curl_easy_init(); // Initialize libcurl
+ curl_easy_setopt(curl, CURLOPT_CAINFO, getenv("CURL_CA_BUNDLE"));
+ curl_easy_setopt(curl, CURLOPT_URL, "https://test22.free.beeceptor.com"); // Set the URL
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"a\": 2}");
+ struct curl_slist *headers = NULL;
+ headers = curl_slist_append(headers, "Content-Type: application/json");
+ headers = curl_slist_append(headers, "Accept: application/json");
+
+ curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
+ errbuf[0] = 0;
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); // Set callback to handle data
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); // Pass the response string
+ CURLcode res = curl_easy_perform(curl);
+ if (res == CURLE_OK) {
+ result = "response: " + response;
+ } else {
+ std::string errorStr = errbuf[0] ? std::string(errbuf) : curl_easy_strerror(res);
+ result = "curl error: " + errorStr;
+ }
+
+ curl_slist_free_all(headers);
+ curl_easy_cleanup(curl);
+
+ return result;
}License
This project includes the precompiled CURL library, which is distributed under the CURL License.
CURL Homepage: https://curl.se/
