Results 1 to 7 of 7

Thread: Redmine Rest API

  1. #1
    Join Date
    Jan 2016
    Posts
    54
    Thanks
    7
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Redmine Rest API

    Hii everyone

    I'm using Redmine Rest API for my project, I can create, update, delete, ... (Users, Projects, ...) using POST, GET, PUT, DELETE

    I want to get the response from Redmine if create (for example) was OK

    From Redmine documentation :
    Response:

    201 Created: user was created
    422 Unprocessable Entity: user was not created due to validation failures (response body contains the error messages)
    Redmine REST API screenshot : Capture du 2016-05-23 12:23:19.png

    Qt Code :
    Qt Code:
    1. QNetworkReply *reply = manager->post(request, dataByteArray);
    2. /* The finished() signal is triggered once an HTTP request is complete, by connect method we are connecting finished signal with the slot method onRequestFinished(user defined method). If it returns true it means connection is successful else not. You can process the response of the call in this user defined method */
    3. result = connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onRequestFinished(QNetworkReply*)));
    4.  
    5. // Create user : ok
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void TestClass::onRequestFinished(QNetworkReply* reply)
    2. {
    3. QString strReply = reply->readAll();
    4. qDebug() << strReply;
    5. }
    To copy to clipboard, switch view to plain text mode 

    strReply : contains the json response : name, email, ...

    So how can I get the response from Redmine : 201 Created or 422 Unprocessable Entity ??

    Thanks
    Last edited by Binary01; 23rd May 2016 at 12:48.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Redmine Rest API


  3. #3
    Join Date
    Jan 2016
    Posts
    54
    Thanks
    7
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Redmine Rest API

    Hii,

    Function createRedmineUser :
    Qt Code:
    1. QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    2. QString resultString ="{\"user\": {\"login\": \""+login+"\",\"firstname\": \""+firstname+"\",\"lastname\": \""+lastname+"\",\"mail\": \""+email+"\", \"password\": \""+password+"\"}}";
    3. qDebug() << "string to post to redmine : " << resultString;
    4. QByteArray dataByteArray(resultString.toLatin1());
    5. QUrl url("http://redmine-user.rhcloud.com/users.json");
    6. QNetworkRequest request(url);
    7. QByteArray postDataSize = QByteArray::number(dataByteArray.size());
    8. request.setRawHeader("User-Agent", "ApplicationNameV01");
    9. request.setRawHeader("Content-Type", "application/json");
    10. request.setRawHeader("Content-Length", postDataSize);
    11. if (manager)
    12. {
    13. bool result;
    14. QNetworkReply *reply = manager->post(request, dataByteArray);
    15. /* The finished() signal is triggered once an HTTP request is complete, by connect method we are connecting finished signal with the slot method onRequestFinished(user defined method). If it returns true it means connection is successful else not. You can process the response of the call in this user defined method */
    16. result = connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onRequestFinished(QNetworkReply*)));
    17. qDebug() << "Connection is success : ? : " << result;
    18. if (reply)
    19. {
    20. qDebug() << "Reply from server is " << reply;
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void TestClass::onRequestFinished(QNetworkReply* reply)
    2. {
    3. QString strReply = reply->readAll();
    4. qDebug() << strReply;
    5.  
    6. // strReply contains json informations see sceenshot
    7. }
    To copy to clipboard, switch view to plain text mode 

    Console out :
    string to post to redmine : "{\"user\": {\"login\": \"userid\",\"firstname\": \"userfname\",\"lastname\": \"userlname\",\"mail\": \"user@gmail.com\", \"password\": \"newuser123\"}}"
    Connection is success : ? : true
    Reply from server is QNetworkReplyHttpImpl(0x9249ef0)
    "{\"user\":{\"id\":36,\"login\":\"userid\",\"first name\":\"userfname\",\"lastname\":\"userlname\",\" mail\":\"user@gmail.com\",\"created_on\":\"2016-05-23T15:06:56Z\",\"api_key\":\"3a628190f278342068a2b 07f37c2df8b85592a51\",\"status\":1}}"
    From doc : http://www.redmine.org/projects/redmine/wiki/Rest_Users
    The response :
    Response:

    201 Created: user was created
    422 Unprocessable Entity: user was not created due to validation failures (response body contains the error messages)
    Why I get JSON response ( see Console out ) and not this code :
    201 Created: user was created
    422 Unprocessable Entity: user was not created due to validation failures (response body contains the error messages)
    Sceenshot = Console out : Capture du 2016-05-23 16:08:11.jpg
    Last edited by Binary01; 23rd May 2016 at 17:32.

  4. #4
    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: Redmine Rest API

    Quote Originally Posted by Binary01 View Post
    Why I get JSON response ( see Console out ) and not this code :
    You do not check the response, so how do you know you are not getting it?

    Cheers,
    _

  5. #5
    Join Date
    Jan 2016
    Posts
    54
    Thanks
    7
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Redmine Rest API

    You do not check the response, so how do you know you are not getting it?
    How can I check the response ?

  6. #6
    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: Redmine Rest API

    Quote Originally Posted by Binary01 View Post
    How can I check the response ?
    One little trick in getting help on a forum is to read the replies to one's question.
    One little trick in hypertext based systems is to follow links.
    One little trick in using API documentations is to look at descriptions of methods and enums.

    Combined they make a huge trick of finding that there is a way to get attributes from a network reply's request object and that there is an attribute called QNetworkRequest::HttpStatusCodeAttribute.

    After a bit of practising the little tricks individually, they will eventually allow you to perform the huge trick on your own.

    Cheers,
    _

  7. #7
    Join Date
    Dec 2015
    Posts
    35
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Redmine Rest API

    Try to use this
    Qt Code:
    1. void TestClass::onRequestFinished(QNetworkReply* reply)
    2. {
    3.  
    4. if(reply->error())
    5. {
    6. qDebug() << "ERROR : " << reply->errorString();
    7. }
    8. else
    9. {
    10. qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    11. qDebug() << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
    12.  
    13. QString strReply = reply->readAll();
    14. qDebug() << strReply;
    15. // ....
    16.  
    17.  
    18. }
    19. reply->deleteLater();
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. connect to rest service with qt and error SSL handshake failed
    By panagiotisss in forum Qt Programming
    Replies: 5
    Last Post: 24th September 2015, 09:56
  2. Rest Web Service - Keep-alive Connection
    By buleron in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 29th May 2015, 15:54
  3. Replies: 1
    Last Post: 15th June 2012, 12:45
  4. Replies: 12
    Last Post: 22nd April 2012, 21:48
  5. Removing divider between menubar and rest of main window
    By tgreaves in forum Qt Programming
    Replies: 4
    Last Post: 18th February 2009, 19:34

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.