Results 1 to 4 of 4

Thread: Send JSON & Key via HTTP POST

  1. #1
    Join Date
    Jun 2014
    Posts
    14
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Send JSON & Key via HTTP POST

    Having trouble sending JSON & Key via POST

    Qt Code:
    1. QByteArray jsonString = "{}";
    2.  
    3. QByteArray postDataSize = QByteArray::number(jsonString.size());
    4.  
    5. QUrl req("webpagehere");
    6. req.setQuery("Key=XXXXXXXXXXXXX");
    7.  
    8. QNetworkRequest request(req);
    9.  
    10. request.setRawHeader("User-Agent", "Test");
    11. request.setRawHeader("X-Custom-User-Agent", "Test");
    12. request.setRawHeader("Content-Type", "application/json");
    13. request.setRawHeader("Content-Length", postDataSize);
    14.  
    15. QNetworkAccessManager test;
    16.  
    17. QEventLoop loop;
    18. connect(&test, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
    19. QNetworkReply * reply = test.post(request, jsonString);
    20. connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError(QNetworkReply::NetworkError)));
    21. loop.exec();
    22.  
    23. QByteArray response = reply->readAll();
    24. qDebug() << response;
    To copy to clipboard, switch view to plain text mode 

    Is there anything wrong with this code? or is it my webpage which is at fault? or possibly both?

    Regards,

    Richard

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Send JSON & Key via HTTP POST

    I would guess that your request's body is not properly formatted and the webserver rejects the request.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,543
    Thanks
    13
    Thanked 1,577 Times in 1,506 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Send JSON & Key via HTTP POST

    You do not need to set the Content-Length header yourself, Qt will do that when you call post().

    Are you sure that the server is expecting the key in the query string and raw JSON data in the post body and not:
    • A URL encoded body with multiple parameters encoded (application/x-www-form-urlencoded), or
    • A multipart/form-data payload?


    Are you sure the server is expecting application/json as the content type and not one of the other, non-standard types used for JSON?
    Is the JSON string UTF8 encoded?

    Without seeing any error information or server logs we can only guess.

  4. #4
    Join Date
    Jun 2014
    Posts
    14
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Send JSON & Key via HTTP POST

    Thanks for the replies. The server was expecting form data.

    Have now fixed this to send multipart/form-data and it's working a dream!

    Many Thanks,

    Richard
    Last edited by Poonarge; 21st August 2014 at 12:57.

Similar Threads

  1. Replies: 0
    Last Post: 12th June 2014, 12:37
  2. Replies: 1
    Last Post: 12th April 2013, 19:46
  3. Replies: 1
    Last Post: 6th February 2013, 12:18
  4. Replies: 9
    Last Post: 10th October 2012, 22:55
  5. How do I send data with http post request?
    By Morea in forum Qt Programming
    Replies: 13
    Last Post: 21st January 2009, 22:51

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.