Error in HTTP request a URL and post data on it
-
I want to HTTP request a URL and put data on it
But I get this error after run the code:
Error : "Error transferring http://url.com/api/NMdata/89769CF1-NN21-41L9-AD99-B5882DSAS252 - server replied: Not Found"where is the code problem?
#include <QCoreApplication> #include <QtNetwork> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QNetworkRequest request(QUrl("http://url.com/api/NMdata/89769CF1-NN21-41L9-AD99-B5882DSAS252")); request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json")); QByteArray array("[158,2019/02/10,10:48:51]"); QNetworkAccessManager nam; QNetworkReply *reply = nam.post(request, array); while(!reply->isFinished()) { qApp->processEvents(); } if (reply->error() == QNetworkReply::NoError) { QByteArray response_data = reply->readAll(); // QJsonDocument json1 = QJsonDocument::fromJson(response_data); // QJsonObject object = json1.object(); // qDebug() << "IP: " << object["ip"].toString(); } else{ qDebug() << "Error : " << reply->errorString(); } reply->deleteLater(); return a.exec(); }
In my web browser(like google chrome), I checked the address validation
-
Hi,
How are you doing the post in your browser ?
-
Then why are you doing a post call with QNetworkAccessManager ?
-
@SGaist
I got confused,maybe I'm not sure how can I make my problem understandable.
In my web browser(like google chrome), I checked only the address validation ,
In my code I would request a URLs and put data on it with QNetworkAccessManager
But I get this error:
Error : "Error transferring http://url.com/api/NMdata/89769CF1-NN21-41L9-AD99-B5882DSAS252 - server replied: Not Found" -
@isan said in Error in HTTP request a URL:
In my web browser(like google chrome), I checked only the address validation ,
In my code I would request a URLs and put data on it with QNetworkAccessManagerWhen you visit a page with a web browser, you are using HTTP GET, not HTTP POST.
@SGaist is trying to tell you to replace
QNetworkAccessManager::post()
withQNetworkAccessManager::get()
.See https://www.w3schools.com/tags/ref_httpmethods.asp for a more detailed explanation.
-
@isan said in Error in HTTP request a URL:
In my web browser(like google chrome), I checked only the address validation ,
In my code I would request a URLs and put data on it with QNetworkAccessManagerWhen you visit a page with a web browser, you are using HTTP GET, not HTTP POST.
@SGaist is trying to tell you to replace
QNetworkAccessManager::post()
withQNetworkAccessManager::get()
.See https://www.w3schools.com/tags/ref_httpmethods.asp for a more detailed explanation.
-
@JKSH tnx but I want to send data to a server in my code so I use QNetworkAccessManager::post()
Like I said before , I checked only the address validation ,in my browser because I get server replied: Not Found from the code@isan said in Error in HTTP request a URL and put data on it:
I get server replied: Not Found
Check the documentation of the server.
-
@isan said in Error in HTTP request a URL and put data on it:
I get server replied: Not Found
Check the documentation of the server.
-
@JKSH said in Error in HTTP request a URL and put data on it:
Check the documentation of the server.
My code has not a problem?
where can I check the documentation of the server?@isan said in Error in HTTP request a URL and put data on it:
My code has not a problem?
I don't know.
First, you must find out how the server expects you to send data. Then, you must write your code in a way that your server can understand.
where can I check the documentation of the server?
From the server's website, probably.
@isan said in Error in HTTP request a URL and put data on it:
I checked only the address validation ,in my browser because I get server replied: Not Found from the code
You validated that the server allows HTTP GET at this address. Does the server allow HTTP POST at this address?
-
@isan said in Error in HTTP request a URL and put data on it:
My code has not a problem?
I don't know.
First, you must find out how the server expects you to send data. Then, you must write your code in a way that your server can understand.
where can I check the documentation of the server?
From the server's website, probably.
@isan said in Error in HTTP request a URL and put data on it:
I checked only the address validation ,in my browser because I get server replied: Not Found from the code
You validated that the server allows HTTP GET at this address. Does the server allow HTTP POST at this address?
-
@JKSH I know that the data format should be:
Value, date, time like 166,2019/02 /11,3: 34: 45
The server manager tells me that I can POST data in it@isan a couple of things here...
- Would you mind editing your post title to reflect you're trying to "post" data instead of put
- Are you able to post any JSON data to that URL using any other tool? i.e. curl
See here for an example how to post JSON data from command line with curl just in case. - You should avoid any loop messing with Qt event loop. You should instead connect signals finished() and error(). See here for instance
while(!reply->isFinished()) { qApp->processEvents(); }
- It looks like the array data you're sending is not a valid JSON construct. You can use this online tool to check for JSON validity. That could be the cause of server rejection of your request.
Error: Parse error on line 1: [158, 2019 / 02 / 10, 10: 48: 5 -----------^ Expecting 'EOF', '}', ',', ']', got 'undefined'
-
@isan said in Error in HTTP request a URL and put data on it:
My code has not a problem?
I don't know.
First, you must find out how the server expects you to send data. Then, you must write your code in a way that your server can understand.
where can I check the documentation of the server?
From the server's website, probably.
@isan said in Error in HTTP request a URL and put data on it:
I checked only the address validation ,in my browser because I get server replied: Not Found from the code
You validated that the server allows HTTP GET at this address. Does the server allow HTTP POST at this address?
@JKSH said in Error in HTTP request a URL and post data on it:
You validated that the server allows HTTP GET at this address. Does the server allow HTTP POST at this address?
The server did not allow and the server administrator had made a mistake
thank you -
@isan a couple of things here...
- Would you mind editing your post title to reflect you're trying to "post" data instead of put
- Are you able to post any JSON data to that URL using any other tool? i.e. curl
See here for an example how to post JSON data from command line with curl just in case. - You should avoid any loop messing with Qt event loop. You should instead connect signals finished() and error(). See here for instance
while(!reply->isFinished()) { qApp->processEvents(); }
- It looks like the array data you're sending is not a valid JSON construct. You can use this online tool to check for JSON validity. That could be the cause of server rejection of your request.
Error: Parse error on line 1: [158, 2019 / 02 / 10, 10: 48: 5 -----------^ Expecting 'EOF', '}', ',', ']', got 'undefined'
@Pablo-J.-Rogina thanks
-
@JKSH said in Error in HTTP request a URL and post data on it:
You validated that the server allows HTTP GET at this address. Does the server allow HTTP POST at this address?
The server did not allow and the server administrator had made a mistake
thank you -
@Stephen28
By (a) checking the particular server you are using to do whatever to allowGET
s on the appropriate URL and (b) checking the server application to make sure it acceptsGET
s and responds accordingly.