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. Sending curl command to command prompt using QProcess

Sending curl command to command prompt using QProcess

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++qprocesscurlapi
20 Posts 5 Posters 3.7k Views
  • 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.
  • A Offline
    A Offline
    Ahsan Niaz
    wrote on last edited by
    #7

    i want to send the command to the command prompt using QProcess
    When i send the command to the command prompt it doesn't run perfectly, but when i paste my command on the command prompt manually, it works perfectly.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #8

      Then do what we suggest - add your parameters to a QStringList, pass it to QProcess. And no I won't show you the code, you can show us yours.

      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
      0
      • A Offline
        A Offline
        Ahsan Niaz
        wrote on last edited by
        #9

        This is what I have done to create a command and send it to the command prompt

        char * final = "curl -X POST https://content.dropboxapi.com/2/files/upload --header \"Authorization: Bearer access_token\" --header \"Dropbox-API-Arg: {\\\"path\\\": \\\"/Orders/";
                    s.replace(" ",""); //s has todays date
                    QByteArray ba = s.toLocal8Bit();
                    char *c_str2 = ba.data();
                    char *three = new char[strlen(final) + strlen(c_str2) + 1];
                    strcpy(three, final);
                    strcat(three, c_str2);
                    final = three;
                    char * name = "/United_Plof.CSV\\\"}\" --header \"Content-Type: application/octet-stream\" --data-binary @";
                    char * four = new char[strlen(final) + strlen(name) + 1];
                    strcpy(four, final);
                    strcat(four, name);
                    final = four;
                    ba = united_plof.toLocal8Bit();
                    char * c_str3 = ba.data();
                    char *five = new char[strlen(final) + strlen(c_str3) + 1];
                    strcpy(five, final);
                    strcat(five, c_str3);
                    //final = final + auth;
                    final = five;
                    qDebug() << final;
                    QProcess mproc;
                    mproc.start(final);
        
        
                    mproc.waitForFinished();
                     qDebug() <<  mproc.errorString();
                    QByteArray output = mproc.readAll();
                    qDebug().noquote() << output;
                    
        

        The (final) command that appears in output is

        curl -X POST https://content.dropboxapi.com/2/files/upload --header "Authorization: Bearer access_token" --header "Dropbox-API-Arg: {\"path\": \"/Orders/SatNov142020/United_Plof.CSV\"}" --header "Content-Type: application/octet-stream" --data-binary @C:/Users/U/Dropbox/order/United/2020_Sep_06_06_29_29__CSPROD_N.CSV
        

        I have sent this command to Qprocess.start, but it gave me the following error

        Error in call to API function "files/upload": HTTP header "Dropbox-API-Arg": could not decode input as JSON
        

        To verify that my command is right, i copied the curl command from output and executed it in the command prompt manually, it worked fine.
        I hope i am being very clear here.

        JonBJ 1 Reply Last reply
        0
        • A Ahsan Niaz

          This is what I have done to create a command and send it to the command prompt

          char * final = "curl -X POST https://content.dropboxapi.com/2/files/upload --header \"Authorization: Bearer access_token\" --header \"Dropbox-API-Arg: {\\\"path\\\": \\\"/Orders/";
                      s.replace(" ",""); //s has todays date
                      QByteArray ba = s.toLocal8Bit();
                      char *c_str2 = ba.data();
                      char *three = new char[strlen(final) + strlen(c_str2) + 1];
                      strcpy(three, final);
                      strcat(three, c_str2);
                      final = three;
                      char * name = "/United_Plof.CSV\\\"}\" --header \"Content-Type: application/octet-stream\" --data-binary @";
                      char * four = new char[strlen(final) + strlen(name) + 1];
                      strcpy(four, final);
                      strcat(four, name);
                      final = four;
                      ba = united_plof.toLocal8Bit();
                      char * c_str3 = ba.data();
                      char *five = new char[strlen(final) + strlen(c_str3) + 1];
                      strcpy(five, final);
                      strcat(five, c_str3);
                      //final = final + auth;
                      final = five;
                      qDebug() << final;
                      QProcess mproc;
                      mproc.start(final);
          
          
                      mproc.waitForFinished();
                       qDebug() <<  mproc.errorString();
                      QByteArray output = mproc.readAll();
                      qDebug().noquote() << output;
                      
          

          The (final) command that appears in output is

          curl -X POST https://content.dropboxapi.com/2/files/upload --header "Authorization: Bearer access_token" --header "Dropbox-API-Arg: {\"path\": \"/Orders/SatNov142020/United_Plof.CSV\"}" --header "Content-Type: application/octet-stream" --data-binary @C:/Users/U/Dropbox/order/United/2020_Sep_06_06_29_29__CSPROD_N.CSV
          

          I have sent this command to Qprocess.start, but it gave me the following error

          Error in call to API function "files/upload": HTTP header "Dropbox-API-Arg": could not decode input as JSON
          

          To verify that my command is right, i copied the curl command from output and executed it in the command prompt manually, it worked fine.
          I hope i am being very clear here.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #10

          @Ahsan-Niaz said in Sending curl command to command prompt using QProcess:

          and send it to the command prompt

          You're not sending it "to the command prompt".

          You are passing a single string to QProcess::start() overload, which either has already been deprecated/removed or soon will be, apparently. So we are being instructed to move away from asking Qt QProcess::start() to execute a string, which relies on its argument-splitting, and rather to pass separate arguments explicitly.

          Your issue may or may not lie in this. I would have stuck with your original question

          Can you help me in defining the arguments for the following command?

          and just split up your string into the necessary, separate QStringList() arguments to be passed to void QProcess::start(const QString &program, const QStringList &arguments, QIODevice::OpenMode mode = ReadWrite). This si what @SGaist & @Christian-Ehrlicher were also recommending. It would be a lot cleaner than all your strcpy() stuff. It may reveal why there is a problem with the Dropbox-API-Arg argument.

          Untested, but the items to construct the QStringList from C++ code looks to me like they will be:

          "-X"
          "POST"
          "https://content.dropboxapi.com/2/files/upload"
          "--header"
          "Authorization: Bearer access_token"
          "--header"
          "Dropbox-API-Arg: {\"path\": \"/Orders/FriNov132020/United_Plof.CSV\"}"
          "--header"
          "Content-Type:application/octet-stream"
          "--data-binary"
          "@C:/Users/U/Dropbox/order/United/2020_Sep_06_06_29_29__CSPROD_N.CSV"
          
          1 Reply Last reply
          1
          • A Offline
            A Offline
            Ahsan Niaz
            wrote on last edited by
            #11

            @JonB Hi, I tried to do what you have suggested

            QStringList arg;
                        arg << "-X" << "https://content.dropboxapi.com/2/files/upload" << "--header" << "Authorization: Bearer access_token" << "--header" << fourr << "--header" <<"Content-Type:application/octet-stream"<<"--data-binary"<<fivee;
                        qDebug() << arg;
                        mproc.start("curl",arg);
            
            
                        mproc.waitForFinished();
                        QByteArray output = mproc.readAll();
                        
                          qDebug().noquote() << output;             
                         //four = "Dropbox-API-Arg: {\"path\": \"/Orders/SatNov142020/United_Plof.CSV\\\"}\""
                         // five = @C:/Users/U/Dropbox/order/United/2020_Sep_07_06_12_25__CSPROD_N.CSV"
            

            I received nothing, no output,

            JonBJ 1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #12

              QProcess has waitForStart() with a return value, exitCode() and exitStatus() and some more which you should check.

              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
              0
              • A Offline
                A Offline
                Ahsan Niaz
                wrote on last edited by
                #13

                @Christian-Ehrlicher exitcode returns (2)
                and exitStatus returns QProcess::NormalExit

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #14

                  So you should check what this return code tells you - man curl.
                  Can you show us how you initialize 'four' ? The debug output doesn't look correct due to the quoted "
                  You also forgot the second argument.

                  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
                  0
                  • A Offline
                    A Offline
                    Ahsan Niaz
                    wrote on last edited by Ahsan Niaz
                    #15

                    this is how i get (four)

                    char * x = "Dropbox-API-Arg: {\"path\": \"/Orders/";
                                ba = s.toLocal8Bit();    //s has todays date
                                char *c_str6 = ba.data();
                                char *threee = new char[strlen(x) + strlen(c_str6) + 1];
                                strcpy(threee, x);
                                strcat(threee, c_str6);
                                name = "/United_Plof.CSV\\\"}\"";
                                char * fourr = new char[strlen(threee) + strlen(name) + 1];
                                strcpy(fourr, threee);
                                strcat(fourr, name);
                    

                    Do you want me to check (man curl) in qprocess.start?
                    the only thing I wanted is to execute a command on a terminal or command prompt. the command is generated in qt code.

                    1 Reply Last reply
                    0
                    • A Ahsan Niaz

                      @JonB Hi, I tried to do what you have suggested

                      QStringList arg;
                                  arg << "-X" << "https://content.dropboxapi.com/2/files/upload" << "--header" << "Authorization: Bearer access_token" << "--header" << fourr << "--header" <<"Content-Type:application/octet-stream"<<"--data-binary"<<fivee;
                                  qDebug() << arg;
                                  mproc.start("curl",arg);
                      
                      
                                  mproc.waitForFinished();
                                  QByteArray output = mproc.readAll();
                                  
                                    qDebug().noquote() << output;             
                                   //four = "Dropbox-API-Arg: {\"path\": \"/Orders/SatNov142020/United_Plof.CSV\\\"}\""
                                   // five = @C:/Users/U/Dropbox/order/United/2020_Sep_07_06_12_25__CSPROD_N.CSV"
                      

                      I received nothing, no output,

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #16

                      @Ahsan-Niaz said in Sending curl command to command prompt using QProcess:

                      //four = "Dropbox-API-Arg: {\"path\": \"/Orders/SatNov142020/United_Plof.CSV\\\"}\""

                      This at least looks wrong. You should be trying to get the fundamentals right: walk before you can run.

                      1. Do it with literal-strings first. I did that from the strings you showed me earlier. Putting in variables right now is error-prone. When you have the simplest example working correctly is the time to introduce those.

                      2. If you want to know how your QProcess code is sending the final arguments, write a one-line C/C++ program to just print out received argv contents, and use that as your curl command.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        Ahsan Niaz
                        wrote on last edited by Ahsan Niaz
                        #17

                        @JonB
                        I did it in literal strings as well with the following piece of code

                        QStringList arg;
                                    arg << "-X" << "https://content.dropboxapi.com/2/files/upload" << "--header" << "Authorization: Bearer access_token" << "--header" << "Dropbox-API-Arg: {\"path\": \"/Orders/FriNov132020/United_Plof.CSV\"}" << "--header" <<"Content-Type:application/octet-stream"<<"--data-binary"<<"@C:/Users/U/Dropbox/order/United/2020_Sep_06_06_29_29__CSPROD_N.CSV";
                                    qDebug() << arg;
                                    mproc.start("curl",arg);
                                    mproc.waitForStarted();
                        
                                    mproc.waitForFinished();
                                     qDebug() <<  mproc.exitStatus();
                                    QByteArray output = mproc.readAll();
                                    qDebug().noquote() << output;
                        

                        Still nothing in the output.

                        B 1 Reply Last reply
                        0
                        • A Ahsan Niaz

                          @JonB
                          I did it in literal strings as well with the following piece of code

                          QStringList arg;
                                      arg << "-X" << "https://content.dropboxapi.com/2/files/upload" << "--header" << "Authorization: Bearer access_token" << "--header" << "Dropbox-API-Arg: {\"path\": \"/Orders/FriNov132020/United_Plof.CSV\"}" << "--header" <<"Content-Type:application/octet-stream"<<"--data-binary"<<"@C:/Users/U/Dropbox/order/United/2020_Sep_06_06_29_29__CSPROD_N.CSV";
                                      qDebug() << arg;
                                      mproc.start("curl",arg);
                                      mproc.waitForStarted();
                          
                                      mproc.waitForFinished();
                                       qDebug() <<  mproc.exitStatus();
                                      QByteArray output = mproc.readAll();
                                      qDebug().noquote() << output;
                          

                          Still nothing in the output.

                          B Offline
                          B Offline
                          Bonnie
                          wrote on last edited by
                          #18

                          @Ahsan-Niaz
                          I think you forgot your "POST".

                          And about the obsolete start(command) function, it now uses QProcess::splitCommand to get arguments from the command.
                          I use that to print your command and it seems to get a little messy by getting:

                          Dropbox-API-Arg: {\path\: \/Orders/SatNov142020/United_Plof.CSV\}
                          

                          So I look into the source code of splitCommand and I find this comment:

                          // handle quoting. tokens can be surrounded by double quotes
                          // "hello world". three consecutive double quotes represent
                          // the quote character itself.
                          

                          So instead of \", you need """ to represent the quote character inside double quotes.
                          So I think \\\" should be changed to \"\"\" if you use the obsolete function.
                          Note1: I'm not encouraging using the obsolete function.
                          Note2: Hide your access token...

                          Christian EhrlicherC 1 Reply Last reply
                          0
                          • B Bonnie

                            @Ahsan-Niaz
                            I think you forgot your "POST".

                            And about the obsolete start(command) function, it now uses QProcess::splitCommand to get arguments from the command.
                            I use that to print your command and it seems to get a little messy by getting:

                            Dropbox-API-Arg: {\path\: \/Orders/SatNov142020/United_Plof.CSV\}
                            

                            So I look into the source code of splitCommand and I find this comment:

                            // handle quoting. tokens can be surrounded by double quotes
                            // "hello world". three consecutive double quotes represent
                            // the quote character itself.
                            

                            So instead of \", you need """ to represent the quote character inside double quotes.
                            So I think \\\" should be changed to \"\"\" if you use the obsolete function.
                            Note1: I'm not encouraging using the obsolete function.
                            Note2: Hide your access token...

                            Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #19

                            @Bonnie said in Sending curl command to command prompt using QProcess:

                            @Ahsan-Niaz
                            I think you forgot your "POST".

                            You also forgot the second argument.

                            He does not read the comments so what do you expect?

                            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
                            0
                            • A Offline
                              A Offline
                              Ahsan Niaz
                              wrote on last edited by
                              #20

                              I am really thankful to everyone who replied to me and corrected my mistakes. Your help will help me finish my work quicker.
                              Thanks @Christian-Ehrlicher , @Bonnie @JonB

                              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