Qt post request
-
Then write the methods that will marshal and unmarshal your classes to/from text. You can write QDataStream operators.
-
Hi,
Do you mean QNetworkAccessManager::post ?
-
Please describe your exact setup.
Because you could send these two strings as JSON data in one request, or split that in two.
-
I got it but I have a question for you
Why should I use QByteArray for post in Qt? Can't I use a direct class object instead?
QNetworkReply * post(const QNetworkRequest &request, QIODevice *data)
QNetworkReply * post(const QNetworkRequest &request, const QByteArray &data)
QNetworkReply * post(const QNetworkRequest &request, QHttpMultiPart *multiPart) -
@Raii
You want to post a class object across the network? It doesn't make sense. You can post e.g. aQByteArray
, you could think about serializing the object to bytes (e.g. seeQDataStream
) and deserialize at the other end, but not sure what you're trying to achieve. -
@Raii said in Qt post request:
Why should I use QByteArray for post in Qt?
Because network layer sends bytes over network, it does not know anything about your classes (but you can serialise/deserialise your classes as @JonB mentioned).
But nothing stops you from converting your string into byte array:
https://doc.qt.io/qt-5/qstring.html#toLatin1
https://doc.qt.io/qt-5/qstring.html#toLocal8Bit
https://doc.qt.io/qt-5/qstring.html#toUtf8
Which method to use depends on the encoding of your string and what encoding you want to use to send the data. -
I can send it in c # windows form code example
class sendEmailAll { public String title { get; set; } public String message { get; set; } public void sendMessage(sendEmailAll message) //send to class object { using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://192.168.1.33:8081"); client.DefaultRequestHeaders.Accept.Clear(); HttpResponseMessage response = client.PostAsJsonAsync("/email/sendAll", message).Result; // message is class object } } }
-
-
Then write the methods that will marshal and unmarshal your classes to/from text. You can write QDataStream operators.