Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How should post data to API that has ContentTypeHeader:"text/plain; charset=utf-8" ?
zhmh last edited by
I use this link to find API ContentTypeHeader
qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString();
so for post data I do like:
QNetworkRequest request(QUrl("http://site.com/api/PtData/")); request.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain; charset=utf-8"); QByteArray const data("145,2018/02/16,19:17:23"); QNetworkAccessManager nam; QNetworkReply *reply = nam.post(request, data);
QNetworkRequest request(QUrl(QStringLiteral("http://site.com/api/PtData/"))); request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("text/plain; charset=utf-8")); QString text(QStringLiteral("145,2018/02/16,19:17:23")); QByteArray const data = text.toUtf8(); QNetworkAccessManager nam; QNetworkReply *reply = nam.post(request, data);
But the server replied:
Unsupported Media Type
where is the problem?
From the looks of it, you are trying to send the wrong kind of data to that end point. You should check its documentation to see what it expects.
The fact that it replies with text has nothing to do with what is valid to send to it.
isan last edited by
This post is deleted!
@SGaist am I get the correctly contentTypeheader With that link ?
zhmh last edited by
And am I use contentTypeHeader correctly?
Did you check what the endpoint you are sending requests to requires ?
VRonin last edited by VRonin
Can you show us an example of valid request to your API? Maybe using curl?
Qt tutorials from https://www.bogotobogo.com should be avoided, they are of very low quality and sometimes encourage toxic patterns in code.
@SGaist @VRonin I send post request with postman in google chrome
I set header Content-Type:"application/json"
in body section I choose raw
data is successfully posted to the API and I can see it on the URL
I change my code :
QNetworkRequest request(QUrl("http://site.com/api/PtData/")); request.setRawHeader("Content-Type", "application/json"); QByteArray data("102,2018/02/16,19:17:23"); QNetworkAccessManager nam; QNetworkReply *reply = nam.post(request, data);
I do not get any errors, but when I check the URL, the data is not post(set)on it
Except that what you send is not valid JSON.
@SGaist but I post the same format data that I use in postman
POST /api/PatData/ HTTP/1.1 Host: site.com Content-Type: application/json Cache-Control: no-cache Postman-Token: c31beda7-12bf-d5b8-4945-88895d0727a0 "102,2019/02/21,23:31:21"
You should check the data sent using postman.
I checked the C code in postman the data format must be :
Finally it's post successfuly :-/