Reputation: 3
So I'm making a C program using cJSON and curl.
In the curl process itself, I need to send a GET request to an API, and the request link itself would be this.
Anyway. The request itself is not problematic, but when I tried performing the request via libcurl and putting it in a write function, as written below...
// struct MemoryStruct {
// char *memory;
// size_t size;
// };
size_t write_findroute(void *data, size_t size, size_t nmemb, void *userdata) {
cJSON *responseJSON;
cJSON *status;
size_t realsize = size * nmemb;
// struct MemoryStruct *mem = (struct MemoryStruct *)userdata;
// char *ptr = realloc(mem->memory, mem->size + realsize + 1);
// if(ptr == NULL) {
// fprintf(stderr, "Out of memory!\n");
// return 0;
// }
// mem->memory = ptr;
// memcpy(&(mem->memory[mem->size]), data, realsize);
// mem->size += realsize;
// mem->memory[mem->size] = 0;
fprintf(stderr, "%s\n", data); // This is supposed to print the response JSON.
responseJSON = cJSON_Parse(data);
fprintf(stderr, "%s\n", cJSON_Print(responseJSON)); // This is also supposed to print the response JSON.
return realsize;
Here's where it gets finicky.
cJSON_Print()
line out weird string, which are fixed per case. In this case, sometimes instead of the actual response, printing the responseJSON
variable will result in "7.60473" instead.printf("%s\n", data);
) always works as intended.I'm not quite sure what else would be the cause here, since browsing the cJSON issues page didn't bring up anything similar...
Upvotes: 0
Views: 401