Skip to content

Instantly share code, notes, and snippets.

@zachflower
Created January 24, 2017 17:49
Show Gist options
  • Save zachflower/99408924394cf2532a18dc78df9c794b to your computer and use it in GitHub Desktop.
Save zachflower/99408924394cf2532a18dc78df9c794b to your computer and use it in GitHub Desktop.
void curl_json_push(const char* url, const char* payload, const char* method, const char* username, const char* password) {
pid_t pid;
curl_global_init(CURL_GLOBAL_ALL);
pid = fork();
if ( pid == 0 ) {
CURL *curl;
struct curl_slist *headers = NULL;
if ( (curl = curl_easy_init()) ) {
CURLcode res;
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "User-Agent: OASIS MUD");
headers = curl_slist_append(headers, "X-Netuitive-Api-Options: INJECT_TIMESTAMP");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload);
if ( username != NULL ) {
curl_easy_setopt(curl, CURLOPT_USERNAME, username);
}
if ( password != NULL ) {
curl_easy_setopt(curl, CURLOPT_PASSWORD, password);
}
res = curl_easy_perform( curl );
if(res != CURLE_OK) {
sprintf(log_buf, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
bug(log_buf, 0);
}
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
}
_exit(0);
} else if (pid > 0 ) {
curl_global_cleanup();
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment