MCPcopy Create free account
hub / github.com/azadkuh/qhttp / runWeatherClient

Function runWeatherClient

example/helloworld/main.cpp:117–159  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

115}
116
117void runWeatherClient(const QString& cityName) {
118 using namespace qhttp::client;
119
120 QHttpClient client(qApp);
121 QByteArray httpBody;
122
123 QUrl weatherUrl(QString("http://wttr.in/%1").arg(cityName));
124
125 client.request(qhttp::EHTTP_GET, weatherUrl, [&httpBody](QHttpResponse* res) {
126 // response handler, called when the HTTP headers of the response are ready
127
128 // gather HTTP response data manually
129 res->onData([&httpBody](const QByteArray& chunk) {
130 httpBody.append(chunk);
131 });
132
133 // called when all data in HTTP response have been read.
134 res->onEnd([&]() {
135 // print the XML body of the response
136 qDebug(" %d bytes of body's been written in weather.html\n", httpBody.size());
137 QFile f("weather.html");
138 if ( f.open(QFile::WriteOnly) )
139 f.write(httpBody);
140
141 qApp->quit();
142 });
143
144
145 // just for fun! print headers:
146 qDebug("\n[Headers:]");
147 res->headers().forEach([](auto cit) {
148 qDebug("%s : %s", cit.key().constData(), cit.value().constData());
149 });
150 });
151
152 // set a timeout for making the request
153 client.setConnectingTimeOut(10000, []{
154 qDebug("connecting to HTTP server timed out!");
155 qApp->quit();
156 });
157
158 qApp->exec();
159}
160
161
162#endif // QHTTP_HAS_CLIENT

Callers 1

mainFunction · 0.85

Calls 4

requestMethod · 0.80
forEachMethod · 0.80
setConnectingTimeOutMethod · 0.80
writeMethod · 0.45

Tested by

no test coverage detected