Re: JSON and curl problem
If you want to run a program with multiple arguments then it is better to use the start() method that takes a QStringList for the program arguments.
This allows proper escaping of special characters such as quote signs.
Alternatively to running curl as a child process you could also consider looking at QNetworkAccessManager for doing HTTP calls "natively" in Qt.
Cheers,
_
Re: JSON and curl problem
Thanks, I had tried using the QStringList but got the same result, will revisit again though. I will also try your other suggestions today and report back!!!
Re: JSON and curl problem
Quote:
Originally Posted by
philmacu
Thanks, I had tried using the QStringList but got the same result, will revisit again though. I will also try your other suggestions today and report back!!!
So, I had already tried QProcess with QStringList, but just to be sure I tried it again, and I still get the warning in regards to format error. So I read up on the QNetworkAccessManager class and implemented that, I now get an error <h1>Bad Request</h1>Invalid Request, however the Json statement is correct.
The manufacturers of the Teltonika Router have quite a good wiki, so I am going to post on that and see if they have any information.
Re: JSON and curl problem
Quote:
Originally Posted by
philmacu
My guess is that
I have done something really stupid with an escape character, any help would be appreciated, so my code, I create a QString that holds the curl command:
Code:
jsonAuthenticate = "curl -d";
jsonAuthenticate += " "{ "jsonrpc": "2.0", "id": 1, "method": "call", "params": [ "00000000000000000000000000000000", "session", "login", { "username": "root", "password": "********" } ] }"";
jsonAuthenticate += " [url]http://192.168.2.1/ubus[/url] ";
Assuming jsonAuthenticate is a QString the code above will not compile.
Code:
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I. -I. -isystem /usr/include/qt5 -isystem /usr/include/qt5/QtGui -isystem /usr/include/qt5/QtCore -I. -isystem /usr/include/libdrm -I/usr/lib64/qt5/mkspecs/linux-g++ -o main.o main.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:9:28: error: expected ‘;’ before ‘{’ token
jsonAuthenticate += " "{ "jsonrpc": "2.0", "id": 1, "method": "call", "params": [ "00000000000000000000000000000000", "session", "login", { "username": "root", "password": "********" } ] }"";
^
main.cpp:9:195: warning: statement has no effect [-Wunused-value]
": [ "00000000000000000000000000000000", "session", "login", { "username": "root", "password": "********" } ] }"";
^
make: *** [Makefile:768: main.o] Error 1
You need to escape the double quotes embedded in the double-quoted string, or build the JSON object using QJsonObject and QJsonDocument. It's also possible that the double-quote preceding/trailing the outermost braces {} within the JSON are syntactically incorrect..
Code:
jsonAuthenticate = "curl -d";
jsonAuthenticate += " \"{ \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"call\", \"params\": [ \"00000000000000000000000000000000\", \"session\", \"login\", { \"username\": \"root\", \"password\": \"********\" } ] }\" ";
jsonAuthenticate += " http://192.168.2.1/ubus ";
Re: JSON and curl problem
Hi Chris, thanks for the suggestion, I think I have tried with those quotes escaped, but I will check again, the reason I include the quotes was so I could pass the CURL a string. I will also have a look at the QJSON classes, the whole thing is a rather convoluted way of just getting the Teletonika to send me its signal strength. One other option I have is to do this in a script, the script then is called from my QT application, handles the authentication and gets the signal level, then returns it to the application...
Phil
Re: JSON and curl problem
Major thanks to both #anda_skoa and #Chris_W67, turns out I needed a bit of assistance from both your suggestions, I went back and used the QStringList approach and dropped the extra outer quotation marks on the Json string and it work a treat.
So now onto the next problem!!!!