Convert curl request
-
I need to do the following request within a C++ app (response is json):
curl --header "x-api-key:ABCD" -s https://api.test.se/api/mydata
1. Can I replace
curl
with another protocol ? (This is not mandatory, could be as it is now) 2. Is there a header only c++ lib that can complete this request ? Of course, I know there is REST SDK library, but it is too much trouble for that simple request. Thank you. -
I need to do the following request within a C++ app (response is json):
curl --header "x-api-key:ABCD" -s https://api.test.se/api/mydata
1. Can I replace
curl
with another protocol ? (This is not mandatory, could be as it is now) 2. Is there a header only c++ lib that can complete this request ? Of course, I know there is REST SDK library, but it is too much trouble for that simple request. Thank you.curl isn't a protocol, as such, but a CLI tool for data transfer using urls. The underpinnings are based on libcurl, a C library, that implements the data trasfer. As such, you can make API calls directly from your C++ program. Another option would be to look into popen() e.g.
#include
int main()
{
FILE *pipe = popen("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata", "r");while( // read data from **pipe** is true ) { // process data } fclose(pipe);
}
Keep Calm and Carry On
-
curl isn't a protocol, as such, but a CLI tool for data transfer using urls. The underpinnings are based on libcurl, a C library, that implements the data trasfer. As such, you can make API calls directly from your C++ program. Another option would be to look into popen() e.g.
#include
int main()
{
FILE *pipe = popen("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata", "r");while( // read data from **pipe** is true ) { // process data } fclose(pipe);
}
Keep Calm and Carry On
Hmm ... I tried:
FILE* pipe = _popen("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata", "r");
while (true) { pipe->\_Placeholder; break; } fclose(pipe);
but I cannot retrieved any data from
pipe
... -
Hmm ... I tried:
FILE* pipe = _popen("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata", "r");
while (true) { pipe->\_Placeholder; break; } fclose(pipe);
but I cannot retrieved any data from
pipe
...using a C style
FILE *
you have to use C stdio functions. So for example you could do#include
#includeFILE *pipe = popen( ... );
char *buff = NULL;
size_t blen = 0;
while( getline(&buff, &blen, pipe) > 0) {
// process input data in buff;
}
free(buff);I generally prefer
getline()
tofgets()
when using cstdio because it allocates and grows the input buffer as needed. If you wish to stick with a more C++ style interface, then maybe look into [Boost.Process](https://www.boost.org/doc/libs/1\_79\_0/doc/html/process.html) I think you've mentioned QT as part of your framework, so there's [QProcess](https://doc.qt.io/qt-6/qprocess.html) as well.Keep Calm and Carry On
-
Hmm ... I tried:
FILE* pipe = _popen("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata", "r");
while (true) { pipe->\_Placeholder; break; } fclose(pipe);
but I cannot retrieved any data from
pipe
... -
using a C style
FILE *
you have to use C stdio functions. So for example you could do#include
#includeFILE *pipe = popen( ... );
char *buff = NULL;
size_t blen = 0;
while( getline(&buff, &blen, pipe) > 0) {
// process input data in buff;
}
free(buff);I generally prefer
getline()
tofgets()
when using cstdio because it allocates and grows the input buffer as needed. If you wish to stick with a more C++ style interface, then maybe look into [Boost.Process](https://www.boost.org/doc/libs/1\_79\_0/doc/html/process.html) I think you've mentioned QT as part of your framework, so there's [QProcess](https://doc.qt.io/qt-6/qprocess.html) as well.Keep Calm and Carry On
-
For some reason, I was assuming you were using linux. If you're on windows, there's no
getline
in the C stdio library, so you'll have to use fgets() or another C stdio FILE i/o function. It might be a better choice to use the libcurl API directly. Or another C/C++ client library. A quick google search turned up [Windows HTTP Services - Win32 apps | Microsoft Docs](https://docs.microsoft.com/en-us/windows/win32/winhttp/winhttp-start-page) , but that appears to be a Win32 API, so may not be applicable to your situation. There's also [GitHub - embeddedmz/httpclient-cpp: C++ client for making simple HTTP requests](https://github.com/embeddedmz/httpclient-cpp) which might work for you too. I have not tried this package, so cannot speak to its quality or suitability for any purpose, what so ever. Caveat usor.Keep Calm and Carry On
-
For some reason, I was assuming you were using linux. If you're on windows, there's no
getline
in the C stdio library, so you'll have to use fgets() or another C stdio FILE i/o function. It might be a better choice to use the libcurl API directly. Or another C/C++ client library. A quick google search turned up [Windows HTTP Services - Win32 apps | Microsoft Docs](https://docs.microsoft.com/en-us/windows/win32/winhttp/winhttp-start-page) , but that appears to be a Win32 API, so may not be applicable to your situation. There's also [GitHub - embeddedmz/httpclient-cpp: C++ client for making simple HTTP requests](https://github.com/embeddedmz/httpclient-cpp) which might work for you too. I have not tried this package, so cannot speak to its quality or suitability for any purpose, what so ever. Caveat usor.Keep Calm and Carry On
-
But there is getline in c++: std::getline - cppreference.com[^] I need to try it, then I'll ocme back wih feedback.
std::getline() isn't part of C stdio, its part of C++. The getline with the signature
int getline(char **, size_t *, FILE *)
(i.e C stdio, thus theFILE *
parameter) is not provided by MS in the windows C/C++ environment. Or at least it wasn't with VS 2017. It might have been added since then, but I don't think so.Keep Calm and Carry On
-
std::getline() isn't part of C stdio, its part of C++. The getline with the signature
int getline(char **, size_t *, FILE *)
(i.e C stdio, thus theFILE *
parameter) is not provided by MS in the windows C/C++ environment. Or at least it wasn't with VS 2017. It might have been added since then, but I don't think so.Keep Calm and Carry On
I have tried:
std::ifstream ifs("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata");
while (ifs.good())
{
std::string line;
while (std::getline(ifs, line))
{
std::cout << line.c_str() << std::endl;
}
}Nothing retrieved.
-
I have tried:
std::ifstream ifs("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata");
while (ifs.good())
{
std::string line;
while (std::getline(ifs, line))
{
std::cout << line.c_str() << std::endl;
}
}Nothing retrieved.
std::ifstream
reads files, and does not have a constructor which executes an external program. You should go back to pure C and use the_popen
function. [edit] This all you need:char buffer\[132\]; FILE\* pipette = \_popen("curl --header \\"x-api-key:ABCD\\" -s https://api.test.se/api/mydata", "r"); while (fgets(buffer, 132, pipette)) { printf("%s", buffer); }
[/edit]
-
I have tried:
std::ifstream ifs("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata");
while (ifs.good())
{
std::string line;
while (std::getline(ifs, line))
{
std::cout << line.c_str() << std::endl;
}
}Nothing retrieved.
Of course not, unless you actually have a file in your current directory called curl --header "x-api-key:ABCD" -s https://api.test.se/api/mydata.
Keep Calm and Carry On
-
std::ifstream
reads files, and does not have a constructor which executes an external program. You should go back to pure C and use the_popen
function. [edit] This all you need:char buffer\[132\]; FILE\* pipette = \_popen("curl --header \\"x-api-key:ABCD\\" -s https://api.test.se/api/mydata", "r"); while (fgets(buffer, 132, pipette)) { printf("%s", buffer); }
[/edit]