Results 1 to 6 of 6

Thread: HTTP request POST in QT application

  1. #1
    Join Date
    Jun 2011
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default HTTP request POST in QT application

    Hi,
    I want to send the following oauth HTTP POST request using qt netwok apis.

    url: https://accounts.google.com/o/oauth2/token

    POST /accounts/o8/oauth2/token HTTP/1.1
    Host: www.google.com
    Content-Type: application/x-www-form-urlencoded

    client_id=21302922996.apps.googleusercontent.com&
    client_secret=XTHhXh1SlUNgvyWGwDk1EjXB&
    code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp6&
    redirect_uri=urn:ietf:wgauth:2.0ob&
    grant_type=authorization_code


    Can anybody help me out how to use qt api classes QNetworkRequest,QNetworkAccessManager,QNetworkRepl y for the above HTTP POST request.


    Thanks
    Abdul Moiz

  2. #2
    Join Date
    Oct 2010
    Posts
    55
    Thanks
    1
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    9

    Default Re: HTTP request POST in QT application

    Have you tried something like this:

    Qt Code:
    1. QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    2.  
    3. QUrl url("https://accounts.google.com/o/oauth2/token");
    4. QNetworkRequest request(url);
    5.  
    6. request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    7.  
    8. QUrl params;
    9. params.addQueryItem("client_id", "...");
    10. params.addQueryItem("client_secret", "...");
    11. params.addQueryItem("code", "...");
    12. // etc
    13.  
    14. QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));
    15.  
    16. manager->post(request, params.encodedQuery());
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2011
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: HTTP request POST in QT application

    Hi,
    I have tried with code you have posted but I could not get response from the server i.e the code is not entering into the function replyFinished(QNetworkReply *)

    Can you please help me how to debug and find what actually is the problem.

    Thanks
    ABDUL MOIZ

  4. #4
    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: HTTP request POST in QT application

    You could help yourself by showing what you have done. Is replyFinished(QNetworkReply *) declared as a slot? Are any warnings issued at program start?

  5. #5
    Join Date
    Jun 2011
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: HTTP request POST in QT application

    This is what I am trying:

    DownloadManager:ownloadManager()
    {
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    QUrl url("https://accounts.google.com/o/oauth2/token");
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHead er, "application/x-www-form-urlencoded");
    QUrl params;
    params.addQueryItem("client_id", "example.com");
    params.addQueryItem("client_secret", "example_secret");
    params.addQueryItem("code", "example_code");
    params.addQueryItem("redirect_uri", "urn:ietf:wgauth:2.0ob");
    params.addQueryItem("grant_type", "authorization_code");

    QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyfinished(QNetworkReply *)));

    QNetworkReply *reply=manager->post(request, params.encodedQuery());

    }


    void DownloadManager::replyfinished(QNetworkReply *reply)
    {
    int i;
    QByteArray bytes = reply->readAll(); // bytes
    qDebug("reply received");

    for(i=0;i<=bytes.size();i++)
    qDebug() << bytes.at(i);

    }


    I am not getting any response from server since reply->readAll(); is not returning anything.

    Can you help me out.

    If else can u please tell me how to use KQOauth/QOauth to access google contacts api.

    Thanks in advance

    Regards
    Abdul Moiz

  6. #6
    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: HTTP request POST in QT application

    See your other thread... the answer is probably the same. Also, a finished transfer is not necessarily a successful transfer. Have you looked at QNetworkReply::error()?
    "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.

Similar Threads

  1. Request ID of QNetworkaccessmanager get and post request
    By dineshkumar in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2011, 21:56
  2. HTTP request
    By Trok in forum Qt Programming
    Replies: 9
    Last Post: 5th January 2010, 14:49
  3. How do I send data with http post request?
    By Morea in forum Qt Programming
    Replies: 13
    Last Post: 21st January 2009, 22:51
  4. http request
    By yagabey in forum Qt Programming
    Replies: 3
    Last Post: 28th December 2008, 19:19
  5. POST request to a web service
    By QPlace in forum Qt Programming
    Replies: 3
    Last Post: 6th November 2008, 08:05

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.