How to use curl cmd in c++/qt
-
Why I'm getting in the
output
returned byprocess
the following errors:curl: (6) Could not resolve host: application curl: (3) URL using bad/illegal format or missing URL
The same code works fine when i press
win+r
and typecmd.exe /k curl -H "Accept: application/vnd.github.v3.raw" -L "https://api.github.com/repos/jajabu33/test/branches"
QString Debugger::process(const QString& program, const QStringList& arguments) { QProcess process; QByteArray output; QObject::connect(&process, &QProcess::readyReadStandardOutput, [&] { output += process.readAllStandardOutput(); }); process.setProcessChannelMode(QProcess::MergedChannels); process.start(program, arguments); QEventLoop eventLoop; QObject::connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), &eventLoop, [&] { eventLoop.quit(); }); eventLoop.exec(); return output; } QString output = process("cmd.exe", {"/c curl -H \"Accept: application/vnd.github.v3.raw\" -L \"https://github.com/jajabu33/test/branches\""});
-
Why I'm getting in the
output
returned byprocess
the following errors:curl: (6) Could not resolve host: application curl: (3) URL using bad/illegal format or missing URL
The same code works fine when i press
win+r
and typecmd.exe /k curl -H "Accept: application/vnd.github.v3.raw" -L "https://api.github.com/repos/jajabu33/test/branches"
QString Debugger::process(const QString& program, const QStringList& arguments) { QProcess process; QByteArray output; QObject::connect(&process, &QProcess::readyReadStandardOutput, [&] { output += process.readAllStandardOutput(); }); process.setProcessChannelMode(QProcess::MergedChannels); process.start(program, arguments); QEventLoop eventLoop; QObject::connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), &eventLoop, [&] { eventLoop.quit(); }); eventLoop.exec(); return output; } QString output = process("cmd.exe", {"/c curl -H \"Accept: application/vnd.github.v3.raw\" -L \"https://github.com/jajabu33/test/branches\""});
Regarding the use of curl + cmd.exe + QProcess: Qt has a built-in http client, QNetworkAccessManager.
@Rua3n said in How to use curl cmd in c++/qt:
Why I'm getting in the
output
returned byprocess
the following errors:QString Debugger::process(const QString& program, const QStringList& arguments) { QProcess process; [...] process.start(program, arguments); [...] } QString output = process("cmd.exe", {"/c curl -H \"Accept: application/vnd.github.v3.raw\" -L \"https://github.com/jajabu33/test/branches\""});
The QStringList passed to QProcess::start should have one argument per QString, eg { "/c", "curl", "-H", ...}. Use QProcess::splitCommand() if multiple arguments must be expressed as a space delimited string.
QEventLoop eventLoop; QObject::connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), &eventLoop, [&] { eventLoop.quit(); }); eventLoop.exec();
This is a lot of code to accomplish the same thing as QProcess::waitForFinished(), if the goal is to avoid the main QCoreApplication event loop. If that isn't the goal, introducing nested event loops has a tendency to create subtle problems. Set up a connection to read output, return, and let the main event loop do its thing.
-
Regarding the use of curl + cmd.exe + QProcess: Qt has a built-in http client, QNetworkAccessManager.
@Rua3n said in How to use curl cmd in c++/qt:
Why I'm getting in the
output
returned byprocess
the following errors:QString Debugger::process(const QString& program, const QStringList& arguments) { QProcess process; [...] process.start(program, arguments); [...] } QString output = process("cmd.exe", {"/c curl -H \"Accept: application/vnd.github.v3.raw\" -L \"https://github.com/jajabu33/test/branches\""});
The QStringList passed to QProcess::start should have one argument per QString, eg { "/c", "curl", "-H", ...}. Use QProcess::splitCommand() if multiple arguments must be expressed as a space delimited string.
QEventLoop eventLoop; QObject::connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), &eventLoop, [&] { eventLoop.quit(); }); eventLoop.exec();
This is a lot of code to accomplish the same thing as QProcess::waitForFinished(), if the goal is to avoid the main QCoreApplication event loop. If that isn't the goal, introducing nested event loops has a tendency to create subtle problems. Set up a connection to read output, return, and let the main event loop do its thing.
-
@jeremy_k said in How to use curl cmd in c++/qt:
QString, eg { "/c", "curl", "-H", ...}.
I already tried this, also doesn't work, does it work on your side?
Why is cmd.exe needed anyway?
-
@jeremy_k said in How to use curl cmd in c++/qt:
QString, eg { "/c", "curl", "-H", ...}.
I already tried this, also doesn't work, does it work on your side?
@Rua3n said in How to use curl cmd in c++/qt:
@jeremy_k said in How to use curl cmd in c++/qt:
QString, eg { "/c", "curl", "-H", ...}.
I already tried this, also doesn't work, does it work on your side?
Natural language is ambiguous. Post the code.
-
Regarding the use of curl + cmd.exe + QProcess: Qt has a built-in http client, QNetworkAccessManager.
@Rua3n said in How to use curl cmd in c++/qt:
Why I'm getting in the
output
returned byprocess
the following errors:QString Debugger::process(const QString& program, const QStringList& arguments) { QProcess process; [...] process.start(program, arguments); [...] } QString output = process("cmd.exe", {"/c curl -H \"Accept: application/vnd.github.v3.raw\" -L \"https://github.com/jajabu33/test/branches\""});
The QStringList passed to QProcess::start should have one argument per QString, eg { "/c", "curl", "-H", ...}. Use QProcess::splitCommand() if multiple arguments must be expressed as a space delimited string.
QEventLoop eventLoop; QObject::connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), &eventLoop, [&] { eventLoop.quit(); }); eventLoop.exec();
This is a lot of code to accomplish the same thing as QProcess::waitForFinished(), if the goal is to avoid the main QCoreApplication event loop. If that isn't the goal, introducing nested event loops has a tendency to create subtle problems. Set up a connection to read output, return, and let the main event loop do its thing.
@jeremy_k said in How to use curl cmd in c++/qt:
The QStringList passed to QProcess::start should have one argument per QString, eg { "/c", "curl", "-H", ...}.
Just for the record. This works for Windows
cmd /c
but would not under Linux forsh -c
. If the OP finds it easier the following, which would be required under Linux, also works under Windows and saves splitting:process("cmd.exe", {"/c", "curl -H \"Accept: application/vnd.github.v3.raw\" -L \"https://github.com/jajabu33/test/branches\""});
I am with @Christian-Ehrlicher. There are e.g. no shell redirection symbols here. Why bother with
cmd /c
?process("curl", {"-H", "Accept: application/vnd.github.v3.raw", "-L", "https://github.com/jajabu33/test/branches"});
Whether OP really needs curl and a separate OS command when Qt has a built-in http client,
QNetworkAccessManager
, is another matter. -
@jeremy_k said in How to use curl cmd in c++/qt:
The QStringList passed to QProcess::start should have one argument per QString, eg { "/c", "curl", "-H", ...}.
Just for the record. This works for Windows
cmd /c
but would not under Linux forsh -c
. If the OP finds it easier the following, which would be required under Linux, also works under Windows and saves splitting:process("cmd.exe", {"/c", "curl -H \"Accept: application/vnd.github.v3.raw\" -L \"https://github.com/jajabu33/test/branches\""});
I am with @Christian-Ehrlicher. There are e.g. no shell redirection symbols here. Why bother with
cmd /c
?process("curl", {"-H", "Accept: application/vnd.github.v3.raw", "-L", "https://github.com/jajabu33/test/branches"});
Whether OP really needs curl and a separate OS command when Qt has a built-in http client,
QNetworkAccessManager
, is another matter.@JonB said in How to use curl cmd in c++/qt:
process("cmd.exe", {"/c", "curl -H "Accept: application/vnd.github.v3.raw" -L "https://github.com/jajabu33/test/branches""});
This doesnt work on Windows at my side. Im trying to learn how to properly get the cmd output.
-
@JonB said in How to use curl cmd in c++/qt:
process("cmd.exe", {"/c", "curl -H "Accept: application/vnd.github.v3.raw" -L "https://github.com/jajabu33/test/branches""});
This doesnt work on Windows at my side. Im trying to learn how to properly get the cmd output.
@Rua3n
I don't know what "doesn't work" means. Same error as originally? You can guess from the mention ofapplication
that it may not be receiving your command line as intended.Your code for getting the output is correct. You don't check the exit status, which is worth doing, but it can get messed up using
cmd /c
.I suggested an alternative, did you at least try it?
For now would
curl
accept .Accept:application/vnd.github.v3.raw
without a space? Then each arguments could be passed without quotes, might simplify. -
@Rua3n
I don't know what "doesn't work" means. Same error as originally? You can guess from the mention ofapplication
that it may not be receiving your command line as intended.Your code for getting the output is correct. You don't check the exit status, which is worth doing, but it can get messed up using
cmd /c
.I suggested an alternative, did you at least try it?
For now would
curl
accept .Accept:application/vnd.github.v3.raw
without a space? Then each arguments could be passed without quotes, might simplify.@JonB
Does this answer what "doesn't work" means?QString output = process("curl", {"-H", "Accept: application/vnd.github.v3.raw", "-L", "https://github.com/jajabu33/test/branches"});
QString output = process("curl", {"-H ", "\"Accept: application/vnd.github.v3.raw\" ", "-L ", "\"https://github.com/jajabu33/test/branches\""}); curl: option -L : is unknown curl: try 'curl --help' for more information
QString output = process("curl.exe", {"-H \"Accept: application/vnd.github.v3.raw\" -L \"https://github.com/jajabu33/test/branches\""}); curl: (2) no URL specified! curl: try 'curl --help' for more information
QString output = process("curl", {"-H", "\"Accept: application/vnd.github.v3.raw\"", "-L", "\"https://github.com/jajabu33/test/branches\""}); curl: (3) URL using bad/illegal format or missing URL
QString output = process("cmd.exe", {"/c", "curl -H \"Accept: application/vnd.github.v3.raw\" -L \"https://github.com/jajabu33/test/branches\""});
QString output = process("cmd.exe", {"/c \"curl -H \"Accept: application/vnd.github.v3.raw\" -L \"https://github.com/jajabu33/test/branches\"\""}); The system cannot find the path specified.
QString output = process("cmd.exe", {"/c curl -H \"Accept: application/vnd.github.v3.raw\" -L \"https://github.com/jajabu33/test/branches\""}); % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0 0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0curl: (6) Could not resolve host: application curl: (3) URL using bad/illegal format or missing URL
-
@jeremy_k said in How to use curl cmd in c++/qt:
The QStringList passed to QProcess::start should have one argument per QString, eg { "/c", "curl", "-H", ...}.
Just for the record. This works for Windows
cmd /c
but would not under Linux forsh -c
. If the OP finds it easier the following, which would be required under Linux, also works under Windows and saves splitting:process("cmd.exe", {"/c", "curl -H \"Accept: application/vnd.github.v3.raw\" -L \"https://github.com/jajabu33/test/branches\""});
I am with @Christian-Ehrlicher. There are e.g. no shell redirection symbols here. Why bother with
cmd /c
?process("curl", {"-H", "Accept: application/vnd.github.v3.raw", "-L", "https://github.com/jajabu33/test/branches"});
Whether OP really needs curl and a separate OS command when Qt has a built-in http client,
QNetworkAccessManager
, is another matter.@JonB said in How to use curl cmd in c++/qt:
@jeremy_k said in How to use curl cmd in c++/qt:
The QStringList passed to QProcess::start should have one argument per QString, eg { "/c", "curl", "-H", ...}.
Just for the record. This works for Windows
cmd /c
but would not under Linux forsh -c
.Tokenization as an external process is silly, but you are right.
-
@JonB said in How to use curl cmd in c++/qt:
@jeremy_k said in How to use curl cmd in c++/qt:
The QStringList passed to QProcess::start should have one argument per QString, eg { "/c", "curl", "-H", ...}.
Just for the record. This works for Windows
cmd /c
but would not under Linux forsh -c
.Tokenization as an external process is silly, but you are right.
@jeremy_k said in How to use curl cmd in c++/qt:
@JonB said in How to use curl cmd in c++/qt:
@jeremy_k said in How to use curl cmd in c++/qt:
The QStringList passed to QProcess::start should have one argument per QString, eg { "/c", "curl", "-H", ...}.
Just for the record. This works for Windows
cmd /c
but would not under Linux forsh -c
.Tokenization as an external process is silly, but you are right.
What works for Windows?
-
@JonB
Does this answer what "doesn't work" means?QString output = process("curl", {"-H", "Accept: application/vnd.github.v3.raw", "-L", "https://github.com/jajabu33/test/branches"});
QString output = process("curl", {"-H ", "\"Accept: application/vnd.github.v3.raw\" ", "-L ", "\"https://github.com/jajabu33/test/branches\""}); curl: option -L : is unknown curl: try 'curl --help' for more information
QString output = process("curl.exe", {"-H \"Accept: application/vnd.github.v3.raw\" -L \"https://github.com/jajabu33/test/branches\""}); curl: (2) no URL specified! curl: try 'curl --help' for more information
QString output = process("curl", {"-H", "\"Accept: application/vnd.github.v3.raw\"", "-L", "\"https://github.com/jajabu33/test/branches\""}); curl: (3) URL using bad/illegal format or missing URL
QString output = process("cmd.exe", {"/c", "curl -H \"Accept: application/vnd.github.v3.raw\" -L \"https://github.com/jajabu33/test/branches\""});
QString output = process("cmd.exe", {"/c \"curl -H \"Accept: application/vnd.github.v3.raw\" -L \"https://github.com/jajabu33/test/branches\"\""}); The system cannot find the path specified.
QString output = process("cmd.exe", {"/c curl -H \"Accept: application/vnd.github.v3.raw\" -L \"https://github.com/jajabu33/test/branches\""}); % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0 0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0curl: (6) Could not resolve host: application curl: (3) URL using bad/illegal format or missing URL
@Rua3n said in How to use curl cmd in c++/qt:
QString output = process("curl", {"-H ", ""Accept: application/vnd.github.v3.raw" ", "-L ", ""https://github.com/jajabu33/test/branches""});
curl: option -L : is unknownIt's
-L
and not-L
, and the quoting of the url is wrong since the url ishttps://github.com/jajabu33/test/branches
and not"https://github.com/jajabu33/test/branches"
. Same goes for the argument for -H.
@JonB already told you this but you ignored him. -
@Rua3n said in How to use curl cmd in c++/qt:
QString output = process("curl", {"-H ", ""Accept: application/vnd.github.v3.raw" ", "-L ", ""https://github.com/jajabu33/test/branches""});
curl: option -L : is unknownIt's
-L
and not-L
, and the quoting of the url is wrong since the url ishttps://github.com/jajabu33/test/branches
and not"https://github.com/jajabu33/test/branches"
. Same goes for the argument for -H.
@JonB already told you this but you ignored him.@Christian-Ehrlicher
I don't understand what you mean by "already told you this but you ignored him."
told what?-L
doesnt work eitherQString output = process("curl", {" -H", "Accept: application/vnd.github.v3.raw", " -L", "https://github.com/jajabu33/test/branches"}); curl: (3) URL using bad/illegal format or missing URL
QString output = process("curl", {" -H", " Accept: application/vnd.github.v3.raw", " -L", " https://github.com/jajabu33/test/branches"}); curl: (3) URL using bad/illegal format or missing URL
-
@Christian-Ehrlicher
I don't understand what you mean by "already told you this but you ignored him."
told what?-L
doesnt work eitherQString output = process("curl", {" -H", "Accept: application/vnd.github.v3.raw", " -L", "https://github.com/jajabu33/test/branches"}); curl: (3) URL using bad/illegal format or missing URL
QString output = process("curl", {" -H", " Accept: application/vnd.github.v3.raw", " -L", " https://github.com/jajabu33/test/branches"}); curl: (3) URL using bad/illegal format or missing URL
@Rua3n
In both these cases the extra space in" -H"
and" -L"
are wrong.I expect
QString output = process("curl", {"-H", "Accept: application/vnd.github.v3.raw", "-L", "https://github.com/jajabu33/test/branches"});
to work and thought you had shown that it does?
-
@Christian-Ehrlicher
I don't understand what you mean by "already told you this but you ignored him."
told what?-L
doesnt work eitherQString output = process("curl", {" -H", "Accept: application/vnd.github.v3.raw", " -L", "https://github.com/jajabu33/test/branches"}); curl: (3) URL using bad/illegal format or missing URL
QString output = process("curl", {" -H", " Accept: application/vnd.github.v3.raw", " -L", " https://github.com/jajabu33/test/branches"}); curl: (3) URL using bad/illegal format or missing URL
@Rua3n said in How to use curl cmd in c++/qt:
I don't understand what you mean by "already told you this but you ignored him."
Then each arguments could be passed without quotes
-
Rather than continuing to ask for and provide step by step corrections, I suggest using a dummy program in place of curl to show what arguments curl is receiving.
#include <cstdio> int main(int argc, char **argv) { for (int i = 0; i < argc; i++) printf("arg %d is '%s'\n", i, argv[i]); }