QNAM query fails but ReST passes
-
I am using QNetworkAccessManager to access a ReST service. All works fine from the command line using "curl".
However, when run from QNAM the ReST service repies with
"{"message":"invalid method"}"
The URL I pass to QNAM is identical to that provided to curl. Is there some other header/parameter I must set for a ReST query to work?
-
Ok figured it out...(based on some generic 405 error information on StackOverflow). This error is common when using POST instead of GET (or vice versa).
I just assumed they were interchangable - but I was wrong. Well, maybe this will help someone else....
-
Hi,
Whiteout knowing your request nor the QNAM code, it's not possible to help.
-
@SGaist I notice that when I use CURL, the server records a reply with 405 code. But when run from my C code it replies with a 200 code. In case that's a clue.
I have posted my code below...hopefully something stands out...
``` QUrlQuery postData; for (QPair<QString,QString> dataPair : m_queryItems) { postData.addQueryItem(dataPair.first, dataPair.second); } QNetworkRequest request; request.setUrl(QUrl(m_URL)); qDebug() << QUrl(m_URL).toString(); request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json"); if (!m_authUsername.isEmpty()) { // HTTP Basic authentication header value: base64(username:password) QString authInfo = m_authUsername + ":" + m_authPassword; QByteArray authInfoB64 = authInfo.toLocal8Bit().toBase64(); QString authHeaderData = "Basic " + authInfoB64; request.setRawHeader("Authorization", authHeaderData.toLocal8Bit()); } m_NAMobject = new QNetworkAccessManager(); connect(m_NAMobject, &QNetworkAccessManager::finished, this, &TL_RQ_Normal::slot_replyFinished, Qt::QueuedConnection); connect(m_NAMobject, &QNetworkAccessManager::sslErrors, this, &TL_RQ_Normal::slot_checkVersion_SSLErrors, Qt::QueuedConnection); m_replyPtr = m_NAMobject->post(request, postData.toString(QUrl::FullyEncoded).toUtf8());
-
@hskoglund said in QNAM query fails but ReST passes:
usually you'll write some json into the postData.
Especially since this is what @ocgltd told the server the payload was going to be:
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
-
Ok figured it out...(based on some generic 405 error information on StackOverflow). This error is common when using POST instead of GET (or vice versa).
I just assumed they were interchangable - but I was wrong. Well, maybe this will help someone else....
-
O ocgltd has marked this topic as solved on