REST request with an regular expressions in a URL
-
I want to create a GET request using the REST API.
The GET API also understands regular expressions (http://ocalhost:8084/rest/channel/.*/Active.Power ).
But I get an error message when I use this url, because Wireshark told me that the url looks like this: http://ocalhost:8084/rest/channel//ActivePower. And this cannot be computed by the server.
I was not able to accept that request the "".
Does anyone know how to prepare the networkrqeust or the url so that Qt uses the correct url for get request? -
Hi and welcome to devnet,
You are expected to replace the
.*
with something sensible like a string or channel number.Note that this is regular expression is likely wrong as URL usually do not use two slashes one after the other. I would expect something more precise especially when dealing with a REST service.
-
I don't want to replace the '.*' with a regular expression. I want to read all ActivePower and ReactivePower of all channels, see description with of the interface. https://openems.github.io/openems.io/openems/latest/edge/controller.html#_get
-
It works with the Talend API Tester.
-
@Beatnicker said in REST request with an regular expressions in a URL:
The GET API also understands regular expressions (http://ocalhost:8084/rest/channel/.*/Active.Power ).
A URL sent to a server must be a distinct resolvable location, not a wildcard. If you've got a server that's allowing something like that then it's violating the standard.
The URL must distinctly resolve. If you need to send wildcards then they must be supplied as params to the GET/PUT operation.
I looked at the referenced docs and they are doing something that is not going to be well supported. IOW, their API kind of sucks. To do what they suggest in a transportable way they should do http://hostname.com/something?wildcards_allowed_here_but_not_before_question_mark
-
@Beatnicker I think I understand the issue a bit better now.
The way you formulated it made it look like an issue server side where lots of framework allow to define their path using regular expressions like Django's URL dispatcher does.
For example
re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive)
In your case the API does not understand regular expression at all, it's in fact a special path named ".*".
I did a quick test on Python and its HTTPServer class and got the path correctly requested using
QUrl("http://localhost:8080/test/.*/Active.Power")
.I would advise to provide a minimal compilable example using QNetworkAccessManager to ensure we are looking at the correct problem.
Adding the Qt version you are using as well as OS would be a good idea.
Is there any test instance of OpenEMS that can be queried ? Or an easy way to start it locally ?
-
It is not possible to provide you a test instance and it is also not quite easy to set it up quickly
I'm using Qt 5.12.8 und Linux Mint 20.2 Cinnamon and this my example:
#include <QCoreApplication> #include <QNetworkAccessManager> #include <QNetworkReply> #include <QNetworkRequest> #include <QUrl> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QNetworkAccessManager manager; QNetworkRequest request; qWarning() << "http://192.168.0.125:8086/rest/channel/.*/.*Temperature"; QUrl url = QUrl("http://192.168.0.125:8086/rest/channel/.*/.*Temperature"); request.setUrl(url); // HTTP Basic authentication header value: request.setRawHeader("Authorization", "Basic QWRtaW46YWRtaW4=" ); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); QObject::connect(&manager, &QNetworkAccessManager::finished, &a, [](QNetworkReply * reply){ if(reply->error()) { qWarning() << "ERROR!"; qWarning() << reply->errorString(); } else { qWarning() << "OK"; } }); manager.get(request); return a.exec(); }
these are the outputs of the console
http://192.168.0.125:8086/rest/channel/.*/.*Temperature ERROR! "Error transferring http://192.168.0.125:8086/rest/channel/.*/.*Temperature - server replied: Not Found"
and even screenshots from Wireshark
I also take a screenshot from Wireshark with Talent API Tester. You can see that the url transferred correctly as expected.
I hope that I have described the problem even better?
-
@Beatnicker said in REST request with an regular expressions in a URL:
It's interesting that only your first wildcard in your url is being removed when sending via Qt. Not sure why that is though.