jpbarrette/curlpp

Import data from .txt file

MtSimba opened this issue · 4 comments

I am currently working with Influxdb and would like to use curl commands, but in C++. I stumbled upon a problem though.

I have a command call i want to execute like the following statement:

curl -i -XPOST 'http://localhost:8086/write?db='database name'' --data-binary @file.txt

But i am not sure how to do the "@file.txt". curlpp::options::ReadFile didn't work for me, so i was wondering if there was another way to get the import data from the txt file?

Can you explain what is not working with ReadFile. Do you have a code example ?

This is the code i'm using

void InfluxController::Import(std::string input)
{

  try
  {
    // That's all that is needed to do cleanup of used resources (RAII style).
    curlpp::Cleanup cleaner;
    curlpp::Easy request;

    request.setOpt(new curlpp::options::Url("http://localhost:8086/write?db=mydb"));
    request.setOpt(new curlpp::options::Verbose(true));

    request.setOpt(new curlpp::options::PostFields(input));



    // Send request and get a result.
    // By default the result goes to standard output.
    request.perform();
  }

  catch(curlpp::RuntimeError & e)
  {
    std::cout << e.what() << std::endl;
  }

  catch(curlpp::LogicError & e)
  {
    std::cout << e.what() << std::endl;
  }

}

for input i would like to add

std::string msg = "@data.txt";

I know that is not the correct way of doing it. But is it possible to add the file location to data.txt?

If I understand well, you want to send the content of the data.txt file as the body of your POST request.
I'm afraid that passing "@data.txt" to PostFields will just send the "@data.txt" string instead of the file content.
In my opinion, you should open the file, read the content into memory, and pass it to the PostFields option (don't forget to set PostFieldSize).
If file is big, you should probably have to use ReadFunction option.

Thank you for the clarification @sgallou. I think it might be better to just store the content of the file into memory and the pass it to PostFields then.