Network classes

Discuss anything related to product development
Post Reply
crispina
Posts: 22
Joined: Tue Nov 17 2020 2:57 pm

Network classes

Post by crispina »

I have been trying to convert a small Qt HTTP GET request application to CopperSpice which uses the QNetworkRequest, QNetworkReply and QNetworkAccessManager classes to perform the network operations.

In the source files CMakeLists.txt I have included

Code: Select all

target_link_libraries(WeatherTest
   CopperSpice::CsCore
   CopperSpice::CsGui
   CopperSpice::CsNetwork  
)
However, when I run make, I get the following fatal error: QNetworkAccessManager: No such file or directory #include <QNetworkAccessManager>. That is I get fatal errors when including

Code: Select all

#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
I know my Qt HTTP GET request application runs and so being very new to CopperSpice I have probably missed something in the CMakeLists.txt files. Can anyone help with this please?

I looked at the KitchenSink network example but this is a client server chat application which does not use the above classes. I am using Linux Debian 10 and CopperSpice 1.7.0.
barbara
Posts: 447
Joined: Sat Apr 04 2015 2:32 am
Contact:

Re: Network classes

Post by barbara »

We are sorry for the confusion.

The header was called <QNetworkAccessManager> originally and we changed this to <QAccess_Manager> in CS 1.7.0 so the name were not as long and more readable. About ten headers were changed in networking.

Then someone on the team thought the name was hard to read so we changed it again in a posted commit after 1.7.0 was released.

It sounds like we should add <QNetworkAccessManager> back in and have this include forward to "qnetacces_manager.h". It would be nice to have your input since this caused an issue in your code.

Thanks for your patience as we keep improving the library.

Barbara
crispina
Posts: 22
Joined: Tue Nov 17 2020 2:57 pm

Re: Network classes

Post by crispina »

Thank you for your feedback. I had worked out that if I replaced

Code: Select all

#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
with

Code: Select all

#include <qtnetwork.h>
I could build the sample HTTP GET request application. I have now replaced this with

Code: Select all

#include <QAccess_Manager>
#include <QNetwork_Reply>
#include <QNetwork_Request>
as you suggested.

In terms of naming conventions, I think all that is required is to add the header include information to the documentation (unless I have missed it somewhere).

However, when executing the CS version of my Qt GET request application with a simple city wttr weather request in my case, I got the error message

“Unknown network protocol: https”

from my QNetworkReply *reply slot which uses the code shown below

Code: Select all

void MainWindow::managerFinished(QNetworkReply *reply)
{
	if (reply->error() != QNetworkReply::NoError ) {
	//qDebug() << reply->errorString();
	qWarning() <<"ErrorNo: "<< reply->error() << "for url: " << reply->url().toString();
	qDebug() << "Request failed, " << reply->errorString();
	qDebug() << "Headers:"<<  reply->rawHeaderList()<< "content:" << reply->readAll();
	return;
	}
	qDebug()<<"Url: "<<reply->url().toString();    
	QString answer = reply->readAll();
	qDebug()<<"Weather ="<<answer;
	QMessageBox::information(this, "Wttr Weather Service",
	answer);
}
Consequently no data was returned which was not the case with the Qt application. In my Qt code I used a QString to make a https request as shown below.

Code: Select all

void MainWindow::requestWeatherFor(QString city)
{
	 QString wttrStr="https://wttr.in/"+city+"?format=%l:+%C+%t";
           request.setUrl(wttrStr);
           manager->get(request);
}
For CS, the wttrStr was changed to an URL.

Code: Select all

request.setUrl(QUrl(wttrStr));
I tried adding the code line

Code: Select all

request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
only to find I got errors including

class QNetworkRequest’ has no member named ‘setSslConfiguration

This surprised me as the CS API documentation for QNetworkRequest states that there should be a public method called setSslConfiguration.

After some experimenting I changed my get request to

Code: Select all

void MainWindow::requestWeatherFor(QString city)
{
   QUrl url;
    url.setScheme("http");
    url.setHost("wttr.in");
    url.setPath("/"+city);
    QString key = "format";
    QString value=QString::number(4);
    QUrlQuery qurlqr;
    qurlqr.addQueryItem(key, value);
    QString result = qurlqr.toString(QUrl::FullyEncoded);
    qDebug()<<"url parameters: "<<result;

    url.setQuery(result);
    request.setUrl(url);
    manager->get(request);
}
The setScheme uses http (not https). In this code I am sending a GET parameter (format = 4) through the requested URL. Weather data was returned with no errors. It is similar to using curl as shown below

curl http://wttr.in/London?format=4

I thought that this information may be helpful for anyone like me trying to convert their Qt QNetworkAccessManager code to CS. I have done some POST tests using the postman-echo website which just returns a JSON blob of what was sent to it. A working code example is below.

Code: Select all

    QUrl url("http://postman-echo.com/post");
    QByteArray data;
    data.append("key1=14");
    url.setQuery(data);    
    request.setRawHeader("Content-Type", "text/plain");
    request.setUrl(url);
    manager->post(request,data);
However when I try to use https://postman-echo.com/post" I get errors again. Have I overlooked something obvious regarding the use of https like an include path? I have openssl installed and my Qt5 examples do recognize the https url paths correctly.

I have tested OpenWeatherMap as an alternative to wttr using the http protocol and a URL parameter list. I successfully parsed the JSON reply using QJsonDocument to obtain data about temperature, pressure, humidity etc for a specific location. However, again I was not able to use https.

Again thank you for your reply regarding include headers. Some feedback on the above issues would be appreciated.
barbara
Posts: 447
Joined: Sat Apr 04 2015 2:32 am
Contact:

Re: Network classes

Post by barbara »

Thanks for your wonderful help.

(1)
We are working to correct the CopperSpice API and CopperSpice Overview documentation. We should have new docs updated early next week.

(2)
Passing a QString where a QUrl is expected has been disallowed due to type safety. Simply modify your code to explicitly construct a QUrl as you have done. We are adding this to our migration documentation.

(3)
I am using Linux Debian 10 and CopperSpice 1.7.0.
Did you build from source or download our pre-built binary? The method "setSslConfiguration()" is present in the library however it is located inside an "#ifdef QT_SSL". We verified this flag was enabled however it may not have been exported. I have forwarded this information to a CS team member who will look into this further.

Barbara
crispina
Posts: 22
Joined: Tue Nov 17 2020 2:57 pm

Re: Network classes

Post by crispina »

Again thank you for your feedback.
Did you build from source or download our pre-built binaries?
I have been doing my testing using the pre-built binaries downloaded from CS webpage. I have also tried CS 1.6.3 and ran into the same issue when using https.
I have forwarded this information to a CS team member who will look into this further
I look forward to any updates but for now will use http.

Admin Note: We moved the remainder of your question to a new topic. Please refer to "CopperSpice License Question" in the Developers Forum.
barbara
Posts: 447
Joined: Sat Apr 04 2015 2:32 am
Contact:

Re: Network classes

Post by barbara »

We have updated our CopperSpice Overview documentation which includes information about migrating to CS. This updated section includes header name changes.

https://www.copperspice.com/docs/cs_overview/cs-migration.html#source-headers
Post Reply