1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How to pass string to REST API using libcurl in cpp

Discussão em 'Python' iniciado por Stack, Setembro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I want to send string to my rest API call in c++. So, I am using libcurl library.

    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();
    if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "localhost:5000/sample");
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"hi\" : \"there\"}");
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);

    std::cout << res << std::endl;
    }


    O/P from flask API is,


    ImmutableMultiDict([('{"hi" : "there"}', '')])
    ​

    the above code is working:

    I am getting the sent result in my python Flask application.

    but,

    string body = "{\"hi\" : \"there\"}";
    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();
    if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "localhost:5000/sample");
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body);
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);

    std::cout << res << std::endl;
    }


    O/P from flask API is:


    ImmutableMultiDict([('��-ؿ\x7f', '')])
    ​

    this code is not working. the only difference is i am assigning the string to a variable and passing that to curl.

    I am wondering why it's working? how to pass variable to curl?

    Continue reading...

Compartilhe esta Página