Skip to main content

C++

GET

util.h
#ifndef UTIL_H
#define UTIL_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


struct MemoryStruct {
char *memory;
size_t size;
};

size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp);

#endif
util.cpp
#include "util.h"

size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp){
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;

char *ptr = (char*) realloc(mem->memory, mem->size + realsize + 1);
if(ptr == NULL){
printf("error: could not allocate Memory!");
return 0;
}

mem->memory = ptr;
memcpy(&(mem->memory[mem->size]),contents ,realsize);
mem->size +=realsize;
mem->memory[mem->size]= 0;

return realsize;
}
main.cpp
#include <iostream>
#include <string>
#include <curl/curl.h>
#include "util.h"

int main()
{
CURL *curl_handle;
CURLcode res;
struct curl_slist *headerList = NULL;
const char *body = (char *)"{\"options\": { \"method\": \"GET\",\"headers\": {\"Content-Type\": \"application/json\",\"Accept\": \"application/json\"}}, \"url\": \"/rest/api/3/issue/DEV-16\"}";

struct MemoryStruct chunk;
chunk.memory = (char *)malloc (1);
chunk.size = 0;

curl_handle = curl_easy_init();

if(curl_handle){
curl_easy_setopt(curl_handle,CURLOPT_URL, "< YOUR ENDPOINT URL >");
curl_easy_setopt(curl_handle,CURLOPT_USERAGENT, "libcurl-agent/1.0");
//add body
curl_easy_setopt(curl_handle,CURLOPT_POSTFIELDSIZE, strlen(body));
curl_easy_setopt(curl_handle,CURLOPT_POSTFIELDS, body);

//setup callback response
curl_easy_setopt(curl_handle,CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl_handle,CURLOPT_WRITEDATA, (void *) &chunk);

//set custom header list
headerList = curl_slist_append(headerList, "apiKey:< YOURTOKEN >");
curl_easy_setopt(curl_handle,CURLOPT_HTTPHEADER, headerList);

//do request
res = curl_easy_perform(curl_handle);

//log response
if(res != CURLE_OK){
fprintf (stderr, "std error: %s\n", (char *) curl_easy_strerror);
}else {
printf("response size: %lu \n", (unsigned long) chunk.size);
printf("response: %s \n", (char *) chunk.memory);
}

//clear memory
curl_slist_free_all(headerList);
curl_easy_cleanup(curl_handle);
free(chunk.memory);
}

return 0;
}

POST

main.cpp
#include <iostream>
#include <string>
#include <curl/curl.h>
#include "util.h"

int main()
{
CURL *curl_handle;
CURLcode res;
struct curl_slist *headerList = NULL;
const char *body = (char *)"{
\"options\": {
\"method\": \"POST\",
\"headers\": {
\"Content-Type\": \"application/json\",
\"Accept\": \"application/json\"
},
\"body\": {
\"fields\": {
\"project\":{
\"name\": \"<your project name>\"
},
\"summary\": \"Create Issue via POST.\",
\"description\": \"Creating of an issue via a POST request using the API Key Manager\",
\"issuetype\": {
\"id\": \"<your issue type id>\"
}
}
}
},
\"url\": \"/rest/api/2/issue\"
}";

struct MemoryStruct chunk;
chunk.memory = (char *)malloc (1);
chunk.size = 0;

curl_handle = curl_easy_init();

if(curl_handle){
curl_easy_setopt(curl_handle,CURLOPT_URL, "< YOUR ENDPOINT URL >");
curl_easy_setopt(curl_handle,CURLOPT_USERAGENT, "libcurl-agent/1.0");
//add body
curl_easy_setopt(curl_handle,CURLOPT_POSTFIELDSIZE, strlen(body));
curl_easy_setopt(curl_handle,CURLOPT_POSTFIELDS, body);

//setup callback response
curl_easy_setopt(curl_handle,CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl_handle,CURLOPT_WRITEDATA, (void *) &chunk);

//set custom header list
headerList = curl_slist_append(headerList, "apiKey:< YOURTOKEN >");
curl_easy_setopt(curl_handle,CURLOPT_HTTPHEADER, headerList);

//do request
res = curl_easy_perform(curl_handle);

//log response
if(res != CURLE_OK){
fprintf (stderr, "std error: %s\n", (char *) curl_easy_strerror);
}else {
printf("response size: %lu \n", (unsigned long) chunk.size);
printf("response: %s \n", (char *) chunk.memory);
}

//clear memory
curl_slist_free_all(headerList);
curl_easy_cleanup(curl_handle);
free(chunk.memory);
}

return 0;
}