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
Forum Updated to NodeBB v4.3 + New Features

Sending curl command to command prompt using QProcess

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++qprocesscurlapi
20 Posts 5 Posters 4.0k Views 1 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.
  • 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 Online
      Christian EhrlicherC Online
      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 Online
          Christian EhrlicherC Online
          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 Online
                    Christian EhrlicherC Online
                    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