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 "engine/DoricPromise.h"
#include "utils/DoricNetworkService.h"
#include <QCoreApplication>
@ -9,10 +10,20 @@ void DoricNetworkPlugin::request(QString jsValueString, QString callbackId) {
QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8());
QJsonValue jsValue = document.object();
DoricNetworkService::getInstance()->request(jsValue, qApp,
[](int code, QByteArray data) {
qDebug() << code;
qDebug() << data;
qDebug() << "";
});
DoricNetworkService::getInstance()->request(
jsValue, qApp,
[this, callbackId](int code, QList<QByteArray> headers, QByteArray data) {
getContext()->getDriver()->asyncCall(
[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") {
httpRequest.setUrl(QUrl(url));
QNetworkReply *reply = networkAccessManager.get(httpRequest);
@ -44,9 +48,9 @@ public:
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (reply->error() == QNetworkReply::NoError) {
emit response(statusCode, reply->readAll());
emit response(statusCode, reply->rawHeaderList(), reply->readAll());
} else {
emit response(statusCode, QByteArray());
emit response(statusCode, reply->rawHeaderList(), QByteArray());
}
reply->deleteLater();
@ -56,7 +60,7 @@ public:
}
signals:
void run(QJsonValue jsValue);
void response(int code, QByteArray data);
void response(int code, QList<QByteArray> headers, QByteArray data);
};
class DoricNetworkService : public QObject {