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.3k 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.
  • jeremy_kJ jeremy_k

    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.

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #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 jeremy_kJ 2 Replies Last reply
    2
    • JonBJ JonB

      @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 last edited by Rua3n
      #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.

      JonBJ 1 Reply Last reply
      0
      • R Rua3n

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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #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
        0
        • JonBJ JonB

          @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 last edited by Rua3n
          #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
          
          Christian EhrlicherC 1 Reply Last reply
          0
          • JonBJ JonB

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

            jeremy_kJ Offline
            jeremy_kJ Offline
            jeremy_k
            wrote on 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
            0
            • jeremy_kJ jeremy_k

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

                @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
                
                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by Christian Ehrlicher
                #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
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @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 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
                  
                  JonBJ Christian EhrlicherC 2 Replies Last reply
                  0
                  • R Rua3n

                    @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
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #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

                      @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
                      
                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 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
                      • jeremy_kJ Offline
                        jeremy_kJ Offline
                        jeremy_k
                        wrote on 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

                        • Login

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