PDA

View Full Version : Redmine Rest API



Binary01
23rd May 2016, 12:44
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 : 11947

Qt Code :
QNetworkReply *reply = manager->post(request, dataByteArray);
/* 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 */
result = connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onRequestFinished(QNetworkReply*)));

// Create user : ok



void TestClass::onRequestFinished(QNetworkReply* reply)
{
QString strReply = reply->readAll();
qDebug() << strReply;
}

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

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

Thanks

Lesiok
23rd May 2016, 12:51
Read about QNetworkReply::request().

Binary01
23rd May 2016, 16:24
Hii,

Function createRedmineUser :
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QString resultString ="{\"user\": {\"login\": \""+login+"\",\"firstname\": \""+firstname+"\",\"lastname\": \""+lastname+"\",\"mail\": \""+email+"\", \"password\": \""+password+"\"}}";
qDebug() << "string to post to redmine : " << resultString;
QByteArray dataByteArray(resultString.toLatin1());
QUrl url("http://redmine-user.rhcloud.com/users.json");
QNetworkRequest request(url);
QByteArray postDataSize = QByteArray::number(dataByteArray.size());
request.setRawHeader("User-Agent", "ApplicationNameV01");
request.setRawHeader("Content-Type", "application/json");
request.setRawHeader("Content-Length", postDataSize);
if (manager)
{
bool result;
QNetworkReply *reply = manager->post(request, dataByteArray);
/* 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 */
result = connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onRequestFinished(QNetworkReply*)));
qDebug() << "Connection is success : ? : " << result;
if (reply)
{
qDebug() << "Reply from server is " << reply;
}
}


void TestClass::onRequestFinished(QNetworkReply* reply)
{
QString strReply = reply->readAll();
qDebug() << strReply;

// strReply contains json informations see sceenshot
}


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\",\"firstname\":\"userfname\",\"lastname\":\"userlname\",\"mail\":\"user@gmail.com\",\"created_on\":\"2016-05-23T15:06:56Z\",\"api_key\":\"3a628190f278342068a2b07f37c2df8b85592a51\",\"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 : 11948

anda_skoa
23rd May 2016, 17:35
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,
_

Binary01
23rd May 2016, 18:05
You do not check the response, so how do you know you are not getting it?

How can I check the response ?

anda_skoa
23rd May 2016, 19:33
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,
_

RegMe
24th May 2016, 11:00
Try to use this

void TestClass::onRequestFinished(QNetworkReply* reply)
{

if(reply->error())
{
qDebug() << "ERROR : " << reply->errorString();
}
else
{
qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute ).toInt();
qDebug() << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribu te).toString();

QString strReply = reply->readAll();
qDebug() << strReply;
// ....


}
reply->deleteLater();

}