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);
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;
}
}
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;
}
}
To copy to clipboard, switch view to plain text mode
void TestClass::onRequestFinished(QNetworkReply* reply)
{
QString strReply
= reply
->readAll
();
qDebug() << strReply;
// strReply contains json informations see sceenshot
}
void TestClass::onRequestFinished(QNetworkReply* reply)
{
QString strReply = reply->readAll();
qDebug() << strReply;
// strReply contains json informations see sceenshot
}
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
Bookmarks