Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Http get request : no body in webassembly

    Qt for WebAssembly
    wasm http http get emscripten
    2
    10
    1549
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      Mixlu last edited by

      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

      1 Reply Last reply Reply Quote 0
      • Ahmed Yarub Hani Al Nuaimi
        Ahmed Yarub Hani Al Nuaimi @Mixlu last edited by

        @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."

        M 1 Reply Last reply Reply Quote 2
        • Ahmed Yarub Hani Al Nuaimi
          Ahmed Yarub Hani Al Nuaimi last edited by

          You need to use something like websockify or configure CORS. You cannot make requests to any destination just like that.

          1 Reply Last reply Reply Quote 0
          • M
            Mixlu last edited by Mixlu

            @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?

            Ahmed Yarub Hani Al Nuaimi 1 Reply Last reply Reply Quote 0
            • Ahmed Yarub Hani Al Nuaimi
              Ahmed Yarub Hani Al Nuaimi @Mixlu last edited by

              @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?

              M 1 Reply Last reply Reply Quote 0
              • M
                Mixlu @Ahmed Yarub Hani Al Nuaimi last edited by Mixlu

                @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 using manager->sendCustomRequest(...)

                Ahmed Yarub Hani Al Nuaimi 1 Reply Last reply Reply Quote 0
                • Ahmed Yarub Hani Al Nuaimi
                  Ahmed Yarub Hani Al Nuaimi @Mixlu last edited by

                  @Mixlu I understand. I would rather investigate Fetch API or CURL. I used both successfully just yesterday. Otherwise you might file a bug.

                  1 Reply Last reply Reply Quote 0
                  • Ahmed Yarub Hani Al Nuaimi
                    Ahmed Yarub Hani Al Nuaimi last edited by

                    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.

                    1 Reply Last reply Reply Quote 0
                    • M
                      Mixlu last edited by Mixlu

                      @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 ?

                      Ahmed Yarub Hani Al Nuaimi 1 Reply Last reply Reply Quote 0
                      • Ahmed Yarub Hani Al Nuaimi
                        Ahmed Yarub Hani Al Nuaimi @Mixlu last edited by

                        @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."

                        M 1 Reply Last reply Reply Quote 2
                        • M
                          Mixlu @Ahmed Yarub Hani Al Nuaimi last edited by

                          @Ahmed-Yarub-Hani-Al-Nuaimi Oh ok I see, thanks for your time !

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post