Results 1 to 7 of 7

Thread: JSON and curl problem

  1. #1
    Join Date
    Jan 2019
    Posts
    5
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default JSON and curl problem

    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":"711dddfa1d3d391f2a1bcde0f58 4a796","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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  3. #3
    Join Date
    Jan 2019
    Posts
    5
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default 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!!!

  4. #4
    Join Date
    Jan 2019
    Posts
    5
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: JSON and curl problem

    Quote Originally Posted by philmacu View Post
    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.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: JSON and curl problem

    Quote Originally Posted by philmacu View Post
    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:
    Qt Code:
    1. jsonAuthenticate = "curl -d";
    2. jsonAuthenticate += " "{ "jsonrpc": "2.0", "id": 1, "method": "call", "params": [ "00000000000000000000000000000000", "session", "login", { "username": "root", "password": "********" } ] }"";
    3. jsonAuthenticate += " [url]http://192.168.2.1/ubus[/url] ";
    To copy to clipboard, switch view to plain text mode 
    Assuming jsonAuthenticate is a QString the code above will not compile.

    Qt Code:
    1. 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
    2. main.cpp: In function ‘int main(int, char**):
    3. main.cpp:9:28: error: expected ‘;’ before ‘{’ token
    4. jsonAuthenticate += " "{ "jsonrpc": "2.0", "id": 1, "method": "call", "params": [ "00000000000000000000000000000000", "session", "login", { "username": "root", "password": "********" } ] }"";
    5. ^
    6. main.cpp:9:195: warning: statement has no effect [-Wunused-value]
    7. ": [ "00000000000000000000000000000000", "session", "login", { "username": "root", "password": "********" } ] }"";
    8. ^
    9. make: *** [Makefile:768: main.o] Error 1
    To copy to clipboard, switch view to plain text mode 

    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..
    Qt Code:
    1. QString jsonAuthenticate;
    2. jsonAuthenticate = "curl -d";
    3. jsonAuthenticate += " \"{ \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"call\", \"params\": [ \"00000000000000000000000000000000\", \"session\", \"login\", { \"username\": \"root\", \"password\": \"********\" } ] }\" ";
    4. jsonAuthenticate += " http://192.168.2.1/ubus ";
    To copy to clipboard, switch view to plain text mode 
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  6. #6
    Join Date
    Jan 2019
    Posts
    5
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default 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

  7. #7
    Join Date
    Jan 2019
    Posts
    5
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default 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!!!!

Similar Threads

  1. JSon Problem
    By romzxlat in forum Newbie
    Replies: 5
    Last Post: 4th March 2015, 01:14
  2. Problem while creating QOCIDriver: oci.json not found
    By Momergil in forum Qt Programming
    Replies: 3
    Last Post: 28th August 2013, 22:29
  3. Replies: 2
    Last Post: 27th March 2013, 05:21
  4. Problem with curl writefunction
    By januszmk in forum Qt Programming
    Replies: 5
    Last Post: 27th July 2011, 09:17
  5. Replies: 0
    Last Post: 16th May 2010, 18:53

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.