简体   繁体   English

在Qt中设置要发送到服务器的请求字段

[英]Setting request fields in Qt to be sent to server

I have a software which allows user to enter username and password, then submit a request which is sent to my sailsjs server where I want to read it using req.username and req.password . 我有一个允许用户输入用户名和密码,然后提交请求的软件,该请求被发送到我的sailsjs服务器,在这里我想使用req.usernamereq.password读取它。

How do you set the request? 您如何设置请求? Currently I only know how to set header using QNetworkRequest.setRawHeader("username", username) , where server side will access using req.headers.username , which isn't what I want. 目前,我只知道如何使用QNetworkRequest.setRawHeader("username", username)设置标头,服务器端将使用req.headers.username访问,这不是我想要的。

I'm doing this because I already have some codes in my server that reads username using req.username , so I need to find a way to set req fields. 我这样做是因为服务器中已经有一些代码可以使用req.username读取用户名,因此我需要找到一种设置req字段的方法。 However I want to do it from my client side using Qt. 但是我想从客户端使用Qt做到这一点。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks in advance. 提前致谢。

Pass the username and password in jsonString jsonString传递用户名和密码

// Build your JSON string as usual
QByteArray jsonString = "{\"method\":\"AuthenticatePlain\",\"loginName\":\"username@domain.com\",\"password\":\"mypass\"}";

// For your "Content-Length" header
QByteArray postDataSize = QByteArray::number(jsonString.size());

// Time for building your request
QUrl serviceURL("https://www.superService.com/api/1.7/ssapi.asmx");
QNetworkRequest request(serviceURL);

// Add the headers specifying their names and their values with the following method : void QNetworkRequest::setRawHeader(const QByteArray & headerName, const QByteArray & headerValue);
request.setRawHeader("User-Agent", "My app name v0.1");
request.setRawHeader("X-Custom-User-Agent", "My app name v0.1");
request.setRawHeader("Content-Type", "application/json");
request.setRawHeader("Content-Length", postDataSize);

// Use QNetworkReply * QNetworkAccessManager::post(const QNetworkRequest & request, const QByteArray & data); to send your request. Qt will rearrange everything correctly.
QNetworkReply * reply = m_qnam->post(request, jsonString);

Correct format for HTTP POST using QNetworkRequest 使用QNetworkRequest的HTTP POST的正确格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM