How should post data to API that has ContentTypeHeader:"text/plain; charset=utf-8" ?
-
I use this link to find API ContentTypeHeader
this line:
qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString();
print :
"text/plain; charset=utf-8"
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);
or
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?
-
Hi,
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.
-
Hi,
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.
-
Hi,
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.
-
Did you check what the endpoint you are sending requests to requires ?
-
Can you show us an example of valid request to your API? Maybe using curl?
P.S.
Qt tutorials from https://www.bogotobogo.com should be avoided, they are of very low quality and sometimes encourage toxic patterns in code. -
Can you show us an example of valid request to your API? Maybe using curl?
P.S.
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
and data:"122,2019/02/21,10:54:31"
data is successfully posted to the API and I can see it on the URLI 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.
-
You should check the data sent using postman.