add headers in network plugin

This commit is contained in:
王劲鹏 2021-04-26 10:21:38 +08:00 committed by osborn
parent d1a00a716c
commit c74ae5a189
2 changed files with 24 additions and 9 deletions

View File

@ -1,4 +1,5 @@
#include "DoricNetworkPlugin.h" #include "DoricNetworkPlugin.h"
#include "engine/DoricPromise.h"
#include "utils/DoricNetworkService.h" #include "utils/DoricNetworkService.h"
#include <QCoreApplication> #include <QCoreApplication>
@ -9,10 +10,20 @@ void DoricNetworkPlugin::request(QString jsValueString, QString callbackId) {
QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8()); QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8());
QJsonValue jsValue = document.object(); QJsonValue jsValue = document.object();
DoricNetworkService::getInstance()->request(jsValue, qApp, DoricNetworkService::getInstance()->request(
[](int code, QByteArray data) { jsValue, qApp,
qDebug() << code; [this, callbackId](int code, QList<QByteArray> headers, QByteArray data) {
qDebug() << data; getContext()->getDriver()->asyncCall(
qDebug() << ""; [this, callbackId, code, headers, data] {
QMap<QString, QVariant> map;
map.insert("status", code);
map.insert("headers", QVariant::fromValue(headers));
map.insert("data", QString(data));
QVariantList args;
args.append(map);
DoricPromise::resolve(getContext(), callbackId, args);
},
DoricThreadMode::JS);
}); });
} }

View File

@ -35,6 +35,10 @@ public:
} }
} }
if (timeoutVal.isDouble()) {
networkAccessManager.setTransferTimeout(timeoutVal.toInt());
}
if (method == "get") { if (method == "get") {
httpRequest.setUrl(QUrl(url)); httpRequest.setUrl(QUrl(url));
QNetworkReply *reply = networkAccessManager.get(httpRequest); QNetworkReply *reply = networkAccessManager.get(httpRequest);
@ -44,9 +48,9 @@ public:
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (reply->error() == QNetworkReply::NoError) { if (reply->error() == QNetworkReply::NoError) {
emit response(statusCode, reply->readAll()); emit response(statusCode, reply->rawHeaderList(), reply->readAll());
} else { } else {
emit response(statusCode, QByteArray()); emit response(statusCode, reply->rawHeaderList(), QByteArray());
} }
reply->deleteLater(); reply->deleteLater();
@ -56,7 +60,7 @@ public:
} }
signals: signals:
void run(QJsonValue jsValue); void run(QJsonValue jsValue);
void response(int code, QByteArray data); void response(int code, QList<QByteArray> headers, QByteArray data);
}; };
class DoricNetworkService : public QObject { class DoricNetworkService : public QObject {