Function sometimes returns old values when beeing called twice

Viewed 51

I have got an URL encoder which sometimes returns the value from the previous call. But it only happens sometimes...

Caller method of get_encoded_string:

char* call_api(const char* api_key, const char* pokerName, const char* message) {

CURL* curl;
CURLcode res;
curl = curl_easy_init();

char errbuf[CURL_ERROR_SIZE];

/* string builder */
char url[MAX_MESSAGE_SIZE];
snprintf(
    url,
    sizeof(url),
    "%s%s%s%s%s%s",
    "http://xdroid.net/api/message?k=",
    api_key,
    "&t=",
    get_encoded_string(pokerName),
    "&c=",
    get_encoded_string(message));

curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist* headers = NULL;
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
const char* data = "";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
res = curl_easy_perform(curl);

if (res == CURLE_OK) {
    return "success";
}
else {
    return errbuf;
}
curl_easy_cleanup(curl);

Class thats returns previous value:

char* get_encoded_string(char* string_to_encode) {

CURL* curl = curl_easy_init();

/* convert a string into a URL encoded string */
char* encoded_message = curl_easy_escape(curl, string_to_encode, 0);
curl_free(encoded_message);
curl_easy_cleanup(curl);
return encoded_message;

Example: call_api gets called with this values: call_api("api_key", "pokerName","message") so get_encoded_string gets called with "pokerName" and then with "message" But sometimes get_encoded_string returns "pokerName" twice.

Any ideas why this is happening?

1 Answers
#define CURL_STATICLIB
#include <string.h>
#include <Curl/curl.h>
#include "push_notification_api.h"


char* call_api(const char* api_key, const char* pokerName, const char* message) {

    CURL* curl;
    CURLcode res;

    curl = curl_easy_init();

    /* escape the strings for the API call */
    char url[MAX_MESSAGE_SIZE];
    char *esc_name = curl_easy_escape(curl, pokerName, 0);
    char *esc_message = curl_easy_escape(curl, message, 0);

    /* string builder */
    snprintf(
        url,
        sizeof(url),
        "%s%s%s%s%s%s",
        "http://xdroid.net/api/message?k=",
        api_key,
        "&t=",
        esc_name,
        "&c=",
        esc_message);
    curl_free(esc_name);
    curl_free(esc_message);


    char errbuf[CURL_ERROR_SIZE];
    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
    struct curl_slist* headers = NULL;
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    const char* data = "";
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
    res = curl_easy_perform(curl);

    if (res == CURLE_OK) {
        return "success";
    }
    else {
        return errbuf;
    }
    curl_easy_cleanup(curl);
}
Related