Sending curl command to command prompt using QProcess
-
I was trying to send a curl command to terminal or command prompt Firstly, i created the perfect command that should run on cmd, After creating the command, i sent it to terminal but it didn't work. I printed the command using qdebug , copied it from debug output and then executed it on cmd, it worked on cmd but not through the code Here is the curl 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
I copied this command and executed it in cmd, it worked on cmd but not through the code, It generated the following error
Error in call to API function "files/upload": HTTP header "Dropbox-API-Arg": could not decode input as JSON
I used the following code to execute my curl command
QProcess mproc; mproc.start(final); mproc.waitForFinished(); QByteArray output = mproc.readAll(); qDebug().noquote() << output; mproc.close();
Can anybody specify where am i doing a mistake? Any Possible solution?
-
Hi and welcome to devnet,
QProcess::start takes a list of arguments. Pass them through that list, it's usually simpler and cleaner.
-
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
-
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.
-
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.
-
@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.
-
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. -
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.
-
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. -
@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 QtQProcess::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 tovoid 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 yourstrcpy()
stuff. It may reveal why there is a problem with theDropbox-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"
-
@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,
-
QProcess has waitForStart() with a return value, exitCode() and exitStatus() and some more which you should check.
-
@Christian-Ehrlicher exitcode returns (2)
and exitStatus returns QProcess::NormalExit -
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. -
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. -
@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.
-
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.
-
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 receivedargv
contents, and use that as yourcurl
command.
-
-
@JonB
I did it in literal strings as well with the following piece of codeQStringList 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.
-
@Ahsan-Niaz
I think you forgot your "POST".And about the obsolete
start(command)
function, it now usesQProcess::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... -
@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?
-
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