POST request from curl example
-
Hi,
I got example the request like below:curl --request POST \ --url https://demo1.vnetlpr.pl/api/events/search \ --header 'Accept: application/json' \ --header 'Content-Type: application/x-www-form-urlencoded' \ --data public_key=01932182-8a75-7238-9b04-e969bcb9e9e6 \ --data 'hash=MD5( public_key + private_key )' \
do I prepared the same code like above but in in qt ?
QNetworkRequest request; request.setUrl(QUrl("https://demo1.vnetlpr.pl/api/events/search")); request.setRawHeader("Accept", "application/json"); request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); QJsonObject obj; obj["public_key"] = (QString)publicKey; obj["hash"]= (QString)QCryptographicHash::hash(publicKey + privateKey, QCryptographicHash::Md5); QNetworkReply *reply = manager.post(request, QJsonDocument(obj).toJson());
-
You're right. The request body it doesn't in JSON notation.
I rewrote my code like below:
QNetworkRequest request; request.setUrl(QUrl("https://demo1.vcn.pl/api/events/search")); request.setRawHeader("Accept", "application/json"); request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); QByteArray md5 = QCryptographicHash::hash(publicKey + privateKey, QCryptographicHash::Md5); auto ba = QByteArray("public_key="+publicKey+"&hash="+md5); QNetworkReply *reply = manager.post(request,ba);
Now is the same what in example in the first post ?
-
Hi, the .setRawHeader()s look ok but QJson? I don't see any colons etc. in the curl example, the --data lines seem to be vanilla params, so try something like:
auto ba = QByteArray("public_key=01932182-8a75-7238-9b04-e969bcb9e9e6&hash=MD5( public_key + private_key )"); QNetworkReply *reply = manager.post(request, ba);
-
Hi,
@Damian7546 said in POST request from curl example:
obj["public_key"] = (QString)publicKey;
obj["hash"]= (QString)QCryptographicHash::hash(publicKey + privateKey, QCryptographicHash::Md5);Beside the good points made by @hskoglund, this is pretty wrong. You can't type cast a QByteArray as a QString like that.
-
You're right. The request body it doesn't in JSON notation.
I rewrote my code like below:
QNetworkRequest request; request.setUrl(QUrl("https://demo1.vcn.pl/api/events/search")); request.setRawHeader("Accept", "application/json"); request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); QByteArray md5 = QCryptographicHash::hash(publicKey + privateKey, QCryptographicHash::Md5); auto ba = QByteArray("public_key="+publicKey+"&hash="+md5); QNetworkReply *reply = manager.post(request,ba);
Now is the same what in example in the first post ?
-
This post is deleted!
-
Did you check the server logs ?
Did you connect the reply errorOccured and sslErrors signals ? -
-
@SGaist working! Tahnks for help.
-
What did you do to fix the problem ?