PDA

View Full Version : JSON and curl problem



philmacu
26th January 2019, 18:53
Hi Community, I have been wrestling with this most of the day, and yes it is something really stupid that I am doing that is causing my problem, but I can't for the life of me find it!

So I am connecting directly to a Teltonika router that has a SIM in it, there is a Json string that can be sent to get information, the first part of that is to send via curl the following:
curl -d "{ "jsonrpc": "2.0", "id": 1, "method": "call", "params": [ "00000000000000000000000000000000", "session", "login", { "username": "root", "password": "********" } ] }" http://192.168.2.1/ubus

Which returns the following authentication.

{"jsonrpc":"2.0","id":1,"result":[0,{"ubus_rpc_session":"711dddfa1d3d391f2a1bcde0f584a796","timeout":300,"expires":300,"acls":{"access-group":{"superuser":["read","write"],"unauthenticated":["read"]},"ubus":{"*":["*"],"session":["access","login"]},"uci":{"*":["read","write"]}},"data":{"username":"root"}}]}

When I try to send it in Qt via QProcess I get the following parse error
{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}}

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:

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 ";

Then I use QProcess
QString prErrors = "";
QString prStandard = "";
QProcess process;
qDebug() << jsonAuthenticate;
process.start(jsonAuthenticate);
process.waitForFinished(5000);
process.exitStatus();
prErrors = process.readAllStandardError();
prStandard = process.readAllStandardOutput();

I have also tried using extra escape chars on the json string i.e.

"{ "jsonrpc": "2.0", "id": 1, "method": "call", "params": [ "00000000000000000000000000000000", "session", "login", { "username": "root", "password": "********" } ] }"

The plaform I am operating in is Ubuntu16.04 with
Qt Creator 4.2.1
Based on Qt 5.8.0 (GCC 5.3.1 20160406 (Red Hat 5.3.1-6), 64 bit)


All the best...
Phil

anda_skoa
27th January 2019, 11:27
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,
_

philmacu
27th January 2019, 12:25
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!!!

philmacu
27th January 2019, 16:51
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.

ChrisW67
28th January 2019, 00:26
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:


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 ";



Assuming jsonAuthenticate is a QString the code above will not compile.



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


QString jsonAuthenticate;
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 ";

philmacu
28th January 2019, 10:17
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

philmacu
28th January 2019, 18:04
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!!!!