Results 1 to 2 of 2

Thread: Post JSON request in Qt 4.7

  1. #1
    Join Date
    Nov 2016
    Posts
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question Post JSON request in Qt 4.7

    I'm working on a project required me to post JSON request to API server then receive data from server. However I cannot make it work. This is my code
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. QUrl serviceUrl = QUrl("https://www.jusmine.jp/KA/KGBloginCheck");
    7. QNetworkRequest request(serviceUrl);
    8. QJsonObject json;
    9. json.insert("userid","xxxx");
    10. json.insert("userpass","xxxx");
    11. request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
    12. request.setHeader(QNetworkRequest::ContentLengthHeader,QByteArray::number(json.size()));
    13. QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
    14. connect(networkManager,SIGNAL(finished(QNetworkReply*)),this,SLOT(serviceRequestFinish(QNetworkReply*)));
    15. networkManager->post(request,QJsonDocument(json).toJson());
    16. }
    17. void MainWindow::serviceRequestFinish(QNetworkReply *rep)
    18. {
    19. QString strReply = (QString)rep->readAll();
    20. qDebug()<<"Test: "<<strReply;
    21. }
    To copy to clipboard, switch view to plain text mode 
    After running the program I get nothing from server. (This code is built on Qt 4.7.0 so I have to use library qjson4-master). I don't know if any one can help me in this problem.
    This is the result of testing server API:
    Untitled.jpg
    Thank you!
    Last edited by haxuanthao; 2nd November 2016 at 06:56.

  2. #2
    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: Post JSON request in Qt 4.7

    QJsonObject::size() does not return the size of the encoded QJsonDocument object. Leave that header out entirely: QNetworkAccessManager::post() will set it for you.

Similar Threads

  1. Sending Json post request in 5.6
    By d1psy in forum Qt Programming
    Replies: 4
    Last Post: 24th August 2016, 19:59
  2. Correct POST html with JSON data
    By Mbded in forum Qt Programming
    Replies: 11
    Last Post: 23rd August 2015, 10:19
  3. Send JSON & Key via HTTP POST
    By Poonarge in forum Newbie
    Replies: 3
    Last Post: 21st August 2014, 09:13
  4. ASP .NET - POST request method
    By Trok in forum Qt Programming
    Replies: 3
    Last Post: 6th October 2011, 22:52
  5. Request ID of QNetworkaccessmanager get and post request
    By dineshkumar in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2011, 21:56

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.