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 3.7k 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 13 Nov 2020, 23:08 last edited by
    #3

    Hi, I am a newbie in qt creator and I am not aware of many of its datatypes.
    Can you help me in defining the arguments for the following command?

    curl -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
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 14 Nov 2020, 09:50 last edited by
      #4

      Don't know what arguments have to do with QtCreator nor programming c++ - your arguments are the parameters after the function call, separated by spaces. Quoting is also not needed when passing it as QStringList.

      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
      • A Offline
        A Offline
        Ahsan Niaz
        wrote on 14 Nov 2020, 11:35 last edited by
        #5

        I just want to execute the mentioned command on command prompt. I dont want to run any linked c++ code or anything else. Just a command to run on command prompt.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 14 Nov 2020, 11:55 last edited by
          #6

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

          Just a command to run on command prompt.

          So what do you need Qt or c++ for? Open a command prompt, run the command.

          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 14 Nov 2020, 12:07 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
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 14 Nov 2020, 12:14 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 14 Nov 2020, 12:25 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.

                J 1 Reply Last reply 14 Nov 2020, 14:10
                0
                • A Ahsan Niaz
                  14 Nov 2020, 12:25

                  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.

                  J Offline
                  J Offline
                  JonB
                  wrote on 14 Nov 2020, 14:10 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 14 Nov 2020, 14:49 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,

                    J 1 Reply Last reply 14 Nov 2020, 16:03
                    0
                    • C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 14 Nov 2020, 15:03 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 14 Nov 2020, 15:09 last edited by
                        #13

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

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 14 Nov 2020, 15:51 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 14 Nov 2020, 15:52 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
                              14 Nov 2020, 14:49

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

                              J Offline
                              J Offline
                              JonB
                              wrote on 14 Nov 2020, 16:03 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 14 Nov 2020, 16:09 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 14 Nov 2020, 16:54
                                0
                                • A Ahsan Niaz
                                  14 Nov 2020, 16:09

                                  @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 14 Nov 2020, 16:54 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...

                                  C 1 Reply Last reply 14 Nov 2020, 17:27
                                  0
                                  • B Bonnie
                                    14 Nov 2020, 16:54

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

                                    C Offline
                                    C Offline
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on 14 Nov 2020, 17:27 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 14 Nov 2020, 23:38 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

                                      12/20

                                      14 Nov 2020, 15:03

                                      • Login

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