0

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.

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.

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. However I want to do it from my client side using Qt. Any help would be greatly appreciated. Thanks in advance.

YakovL
  • 7,557
  • 12
  • 62
  • 102
Cherple
  • 725
  • 3
  • 10
  • 24

1 Answers1

1

Pass the username and password in 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

MjZac
  • 3,476
  • 1
  • 17
  • 28
  • I did as mentioned but when i did "req.loginName" its undefined. – Cherple Aug 02 '17 at 15:11
  • I see.. this method puts loginName within req.body.loginName, is there a way to put it under req.loginName instead? – Cherple Aug 03 '17 at 08:14
  • @Cherple I am taking your server is in sails js. The parameters sent in post requests are available at `req.params('keyUsed')` else if its an `express app`, the data is available at `req.body.keyUsed`. I am not sure if it can be made available at `req.keyUsed`, unless you write specific middleware to do so. – MjZac Aug 03 '17 at 08:42