QNetworkAccessManager - How to send “PATCH” request

自作多情 提交于 2020-01-23 11:11:08

问题


I am trying to send a "PATCH" request to my firebase application.As far as I read QNetworkManager doesn't support "Patch" request.

How can I send "PATCH" request ?


回答1:


So we are clear that there is no method in QNetworkAccessManager named "patch" Therefore I have used "sendCustomRequest" but with QBuffer. Because QNetworkManager requires a QIODevice object.

QString destination="";
currentNode.replace(QString("/").append(latestNode),"");
destination
        .append(host)
        .append(currentNode)
        .append(".json");
QString jsonString=QString(QString("{").append("\"").append(latestNode).append("\"").append(":").append("\"").append(str).append("\"").append(QString("}")));
QNetworkRequest request(destination);
request.setHeader(QNetworkRequest::ContentTypeHeader,
    "application/x-www-form-urlencoded");
qDebug()<<jsonString;
QBuffer *buffer=new QBuffer();
buffer->open((QBuffer::ReadWrite));
buffer->write(jsonString.toUtf8());
buffer->seek(0);
manager->sendCustomRequest(request,"PATCH",buffer);
qDebug()<<"posted";



回答2:


try:

QNetworkAccessManager* manager = new QNetworkAccessManager();
QNetworkRequest request("http://<domain>/<path>/");
QHttpMultiPart* multipart = new QHttpMultiPart();
//... Add your data in multipart
manager->sendCustomRequest(request, "PATCH", multipart);


来源:https://stackoverflow.com/questions/34065735/qnetworkaccessmanager-how-to-send-patch-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!