Http get request : no body in webassembly
-
Hi,
I want to make a simple HTTP GET request to an API with a simple body.
Here is how I did it:QUrl url("http://192.168.30.222:3000"); QNetworkAccessManager* manager = new QNetworkAccessManager(); QNetworkRequest request; request.setUrl(url); QObject::connect(manager, &QNetworkAccessManager::finished, this, [ = ](QNetworkReply * reply) { qDebug() << "reply: " << reply->readAll(); }); QByteArray bodyData = "BODY DATA"; manager->sendCustomRequest(request, "GET", bodyData);
It is working perfectly under MinGW (Windows), I started Wireshark and saw the "BODY DATA".
But when I compile the project for WebAssembly and make the request with the same project, the body content disappears.I am using:
Qt: 5.14.2
MinGW: 7.3.0
Emscripten: sdk-fastcomp-1.38.27-64bit -
@Mixlu I guess that QUrl is using Fetch API, which in turn (I think) uses XHR. I think that it's a browser restriction. Ref: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send
"If the request method is GET or HEAD, the body parameter is ignored and the request body is set to null." -
You need to use something like websockify or configure CORS. You cannot make requests to any destination just like that.
-
@Ahmed-Yarub-Hani-Al-Nuaimi said in Http get request : no body in webassembly:
You need to use something like websockify or configure CORS. You cannot make requests to any destination just like that.
CORS is configured on my server side to accept all type of request. Do I need to configure something on the client side (here) also?
-
@Mixlu said in Http get request : no body in webassembly:
@Ahmed-Yarub-Hani-Al-Nuaimi said in Http get request : no body in webassembly:
You need to use something like websockify or configure CORS. You cannot make requests to any destination just like that.
CORS is configured on my server side to accept all type of request. Do I need to configure something on the client side (here) also?
Is your Qt app hosted on the same IP and port? I mean in the browser you access it from the same IP?
-
@Ahmed-Yarub-Hani-Al-Nuaimi No, I access the Qt app from a different IP.
But it works well with other request, I am able to send simple HTTP GET/POST request with
manager->get(...), manager->post(...)
It seems that the error only appear when I am usingmanager->sendCustomRequest(...)
-
Oh now I see it! You cannot send a body with a GET request! If you are changing something you'll have to send POST.
-
@Ahmed-Yarub-Hani-Al-Nuaimi said in Http get request : no body in webassembly:
You cannot send a body with a GET request!
Yes it's not really common but why not? The http protocol allow it and the
QNetworkAccessManager->sendCustomRequest(...)
seems to permit it.It is a webassembly restriction ?
-
@Mixlu I guess that QUrl is using Fetch API, which in turn (I think) uses XHR. I think that it's a browser restriction. Ref: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send
"If the request method is GET or HEAD, the body parameter is ignored and the request body is set to null." -
@Ahmed-Yarub-Hani-Al-Nuaimi Oh ok I see, thanks for your time !