How should post data to API that has ContentTypeHeader:"text/plain; charset=utf-8" ?
-
wrote on 20 Feb 2019, 21:35 last edited by
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.
wrote on 21 Feb 2019, 03:54 last edited byThis post is deleted! -
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.
-
wrote on 21 Feb 2019, 14:53 last edited by
And am I use contentTypeHeader correctly?
-
Did you check what the endpoint you are sending requests to requires ?
-
wrote on 21 Feb 2019, 16:49 last edited by VRonin
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.wrote on 21 Feb 2019, 20:00 last edited by zhmh@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.
-
wrote on 21 Feb 2019, 21:39 last edited by zhmh
@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.
-
wrote on 22 Feb 2019, 07:11 last edited by zhmh
I checked the C code in postman the data format must be :
QByteArray data("\"185,2019/02/22,10:59:21\"");
Finally it's post successfuly :-/
1/12