Home > front end >  How to create if statement from curl command output (c )
How to create if statement from curl command output (c )

Time:11-25

I am trying to get the output of the curl command to work inside of an if statement

I am new to C and don't know how I could do this.

int curlreq;
curlreq = system("curl localhost/file.txt");
string curlreqstring = to_string(curlreq);
if ((krxcrlstr.find("hello") != string::npos) ) {
    cout << "hello\n";
}
else if (curlreqstring.find("hello2") != string::npos) {
    cout << "hello2\n";
}

I am doing this on Windows. The project is a console app C 20

All the above code is doing, is printing what the curl response is, but I need that as a variable to then determine what the program should do.

As you see I am getting the contents of a file from localhost, the file itself has a singular line.

CodePudding user response:

std::system returns an int with an implementation-defined value. On many platforms, 0 means success and anything else means some sort of failure. I'm making this assumption in the below example.

My advice is to use which is what the curl command is using internally. With a little setup you can make your program perform curl actions and receive what you get back into your program. If you do not have access to or find it a bit hard to get started with, you could wrap your system command in a function which performs the curl command but directs the output to a temporary file which you read after curl is done.

Example:

#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
// a simple class to remove the temporary file after use
struct file_to_remove {
    // remove "filename" when the object is destroyed
    ~file_to_remove() { std::remove(filename.c_str()); }
    const std::string& str() const { return filename; }
    const std::string filename;
};
// A function to "curl":
std::string Curl(std::string options_plus_url) {
    // An instance to remove the temporary file after use.
    // Place it where you have permission to create files:
    file_to_remove tempfile{"tmpfile"};

    // build the command line
    // -s to make curl silent
    // -o to save to a file
    options_plus_url = "curl -so "   tempfile.str()   " "   options_plus_url;

    // perfom the system() command:
    int rv = std::system(options_plus_url.c_str());

    // not 0 is a common return value to indicate problems:
    if(rv != 0) throw std::runtime_error("bad curl");

    // open the file for reading
    std::ifstream ifs(tempfile.str());

    // throw if it didn't open ok:
    if(!ifs) throw std::runtime_error(std::strerror(errno));

    // put the whole file in the returned string:
    return {std::istreambuf_iterator<char>(ifs),
            std::istreambuf_iterator<char>{}};

} // tmpfile is removed when file_to_remove goes out of scope

With the above Curl function you can perform curl commands and get the response as a std::string which you can then use in your if statements etc.

Example:

int main(int argc, char* argv[]) {
    if(argc < 2) return 1; // must get an URL as argument

    try {
        std::string response = Curl(argv[1]);
        std::cout << response << '\n';

    } catch(const std::exception& ex) {
        std::cout << "Exception: " << ex.what() << '\n';
    }
}
  • Related