Unsolved QNetworkAccessManager to access cloudant REST API
-
I am trying to make a post to a cloudant REST API , but I obtain this reply message:
Satus: "400"
Answer: "{"error":"bad_request","reason":"Referer header required."}\n"
The same request with a REST client, obtains the desired answer from Cloudant.This is the code where the request is constructed:
QNetworkAccessManager m_networkManager;
QNetworkRequest m_request;
QNetworkReply m_reply;
.
.
.
QByteArray jsonPost;
jsonPost = createJsonDocum(cmd);
QUrl url = QUrl(qsurl);
if (url.isValid())
{
m_request.setUrl(url);
m_request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json; charset=utf-8"));
m_request.setHeader(QNetworkRequest::ContentLengthHeader, QByteArray::number(jsonPost.size()));
m_reply = m_networkManager->post(m_request,jsonPost);
}
connect(m_networkManager, SIGNAL(finished(QNetworkReply)),
this, SLOT(replyReceived(QNetworkReply)));And the slot for the reply
void CloudantRESTClient::replyReceived(QNetworkReply * reply)
{
QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
// Print or catch the status code
QString status = status_code.toString(); // or status_code.toInt();
qDebug() << "Satus: "<< status;
QByteArray answer = reply->readAll();
qDebug() << "Answer: "<<answer;
}And this is the json of the post request:
{
"fields": [
"timestamp",
"data.data.data"
],
"selector": {
"deviceId": "BE7A000000002417"
}
}Please, any help would be appreciated.
Idoia
-
@iaguirre from the error message you posted, it looks like your request is missing the "Referer header" the REST server is expecting.
You can use the setRawHeader() method from QNetworkRequest, since that header is not defined in the QNetworkRequest::KnownHeaders enum.
For the right value to set, you stated another client is workingThe same request with a REST client, obtains the desired answer from Cloudant.
so you may need to set that client in "debug" mode or do a network traffic capture to see what it is actually sending for the Referer header.
-
Using insomnia as REST Client I don't see Referer header in the Post Request.
On the other hand, I can't see Qtś Http request because all is encryted :-)Any idea?
-
@iaguirre said in QNetworkAccessManager to access cloudant REST API:
Using insomnia as REST Client I don't see Referer header in the Post Request.
On the other hand, I can't see Qtś Http request because all is encryted :-)@iaguirre think about your reply, why could be that with the same request in both clients (Insomnia and Qt) you can see headers for one and not for the other?
I assume you're using the same network traffic inspection tool for both.
It's strange that if it's "encrypted" on Qt it's not the same for insomnia...Any chances that you post the actual request and JSON data to have a better idea? Please be sure to not disclose usernames/passwords/API keys