Basic authentication with Qt (QNetworkAccessManager)

Just using qNetworkAccessManager normally but add

setRawHeader("Authorization", headerData.toLocal8Bit());

to your request.

Example:

//authentication

QString concatenated = "admin:admin"; //username:password

QByteArray data = concatenated.toLocal8Bit().toBase64();

QString headerData = "Basic " + data;

QNetworkRequest request=QNetworkRequest(QUrl("http://192.168.1.10/getinfo"));

request.setRawHeader("Authorization", headerData.toLocal8Bit());

networkAccessManager->get(request);

`


But if you want to do it by just setting the header value, here's how you can do that:

// HTTP Basic authentication header value: base64(username:password)
QString concatenated = username + ":" + password;
QByteArray data = concatenated.toLocal8Bit().toBase64();
QString headerData = "Basic " + data;
request.setRawHeader("Authorization", headerData.toLocal8Bit());

The recommended way is to connect to the authenticationRequired signal and set the credentials from there.