Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use curl cmd in c++/qt
Forum Updated to NodeBB v4.3 + New Features

How to use curl cmd in c++/qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 4 Posters 1.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Rua3n
    3 Sept 2023, 02:55

    Why I'm getting in the output returned by process 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 type

    cmd.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\""});
    J Offline
    J Offline
    jeremy_k
    wrote on 3 Sept 2023, 06:31 last edited by
    #2

    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 by process 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.

    Asking a question about code? http://eel.is/iso-c++/testcase/

    R J 2 Replies Last reply 3 Sept 2023, 06:39
    0
    • J jeremy_k
      3 Sept 2023, 06:31

      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 by process 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.

      R Offline
      R Offline
      Rua3n
      wrote on 3 Sept 2023, 06:39 last edited by Rua3n 9 Mar 2023, 06:39
      #3

      @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?

      C J 2 Replies Last reply 3 Sept 2023, 07:02
      0
      • R Rua3n
        3 Sept 2023, 06:39

        @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?

        C Online
        C Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 3 Sept 2023, 07:02 last edited by
        #4

        Why is cmd.exe needed anyway?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        2
        • R Rua3n
          3 Sept 2023, 06:39

          @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?

          J Offline
          J Offline
          jeremy_k
          wrote on 3 Sept 2023, 07:11 last edited by
          #5

          @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.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          2
          • J jeremy_k
            3 Sept 2023, 06:31

            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 by process 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.

            J Offline
            J Offline
            JonB
            wrote on 3 Sept 2023, 08:35 last edited by JonB 9 Mar 2023, 08:36
            #6

            @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 for sh -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.

            R J 2 Replies Last reply 3 Sept 2023, 13:43
            2
            • J JonB
              3 Sept 2023, 08:35

              @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 for sh -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.

              R Offline
              R Offline
              Rua3n
              wrote on 3 Sept 2023, 13:43 last edited by Rua3n 9 Mar 2023, 13:45
              #7

              @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.

              J 1 Reply Last reply 3 Sept 2023, 13:59
              0
              • R Rua3n
                3 Sept 2023, 13:43

                @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.

                J Offline
                J Offline
                JonB
                wrote on 3 Sept 2023, 13:59 last edited by JonB 9 Mar 2023, 14:03
                #8

                @Rua3n
                I don't know what "doesn't work" means. Same error as originally? You can guess from the mention of application 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.

                R 1 Reply Last reply 3 Sept 2023, 18:26
                0
                • J JonB
                  3 Sept 2023, 13:59

                  @Rua3n
                  I don't know what "doesn't work" means. Same error as originally? You can guess from the mention of application 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.

                  R Offline
                  R Offline
                  Rua3n
                  wrote on 3 Sept 2023, 18:26 last edited by Rua3n 9 Mar 2023, 18:42
                  #9

                  @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"});
                  

                  8cd0abcf-9c27-40eb-87dc-8c992658329f-image.png


                  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\""});
                  

                  a0514829-61cc-4b56-800c-84eef4a94e6e-image.png


                  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
                  
                  C 1 Reply Last reply 3 Sept 2023, 18:56
                  0
                  • J JonB
                    3 Sept 2023, 08:35

                    @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 for sh -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.

                    J Offline
                    J Offline
                    jeremy_k
                    wrote on 3 Sept 2023, 18:31 last edited by
                    #10

                    @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 for sh -c.

                    Tokenization as an external process is silly, but you are right.

                    Asking a question about code? http://eel.is/iso-c++/testcase/

                    R 1 Reply Last reply 3 Sept 2023, 18:46
                    0
                    • J jeremy_k
                      3 Sept 2023, 18:31

                      @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 for sh -c.

                      Tokenization as an external process is silly, but you are right.

                      R Offline
                      R Offline
                      Rua3n
                      wrote on 3 Sept 2023, 18:46 last edited by
                      #11

                      @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 for sh -c.

                      Tokenization as an external process is silly, but you are right.

                      What works for Windows?

                      1 Reply Last reply
                      0
                      • R Rua3n
                        3 Sept 2023, 18:26

                        @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"});
                        

                        8cd0abcf-9c27-40eb-87dc-8c992658329f-image.png


                        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\""});
                        

                        a0514829-61cc-4b56-800c-84eef4a94e6e-image.png


                        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
                        
                        C Online
                        C Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 3 Sept 2023, 18:56 last edited by Christian Ehrlicher 9 Mar 2023, 18:58
                        #12

                        @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 unknown

                        It's -L and not -L , and the quoting of the url is wrong since the url is https://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.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        R 1 Reply Last reply 3 Sept 2023, 19:02
                        0
                        • C Christian Ehrlicher
                          3 Sept 2023, 18:56

                          @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 unknown

                          It's -L and not -L , and the quoting of the url is wrong since the url is https://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.

                          R Offline
                          R Offline
                          Rua3n
                          wrote on 3 Sept 2023, 19:02 last edited by
                          #13

                          @Christian-Ehrlicher
                          I don't understand what you mean by "already told you this but you ignored him."
                          told what?

                          -L doesnt work either

                          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("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
                          
                          J C 2 Replies Last reply 3 Sept 2023, 19:05
                          0
                          • R Rua3n
                            3 Sept 2023, 19:02

                            @Christian-Ehrlicher
                            I don't understand what you mean by "already told you this but you ignored him."
                            told what?

                            -L doesnt work either

                            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("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
                            
                            J Offline
                            J Offline
                            JonB
                            wrote on 3 Sept 2023, 19:05 last edited by JonB 9 Mar 2023, 19:06
                            #14

                            @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?

                            1 Reply Last reply
                            0
                            • R Rua3n
                              3 Sept 2023, 19:02

                              @Christian-Ehrlicher
                              I don't understand what you mean by "already told you this but you ignored him."
                              told what?

                              -L doesnt work either

                              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("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
                              
                              C Online
                              C Online
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 3 Sept 2023, 19:09 last edited by
                              #15

                              @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."

                              @JonB

                              Then each arguments could be passed without quotes

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              1
                              • J Offline
                                J Offline
                                jeremy_k
                                wrote on 3 Sept 2023, 19:46 last edited by
                                #16

                                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]);
                                }
                                

                                Asking a question about code? http://eel.is/iso-c++/testcase/

                                1 Reply Last reply
                                0

                                11/16

                                3 Sept 2023, 18:46

                                • Login

                                • Login or register to search.
                                11 out of 16
                                • First post
                                  11/16
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved