Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. http post data encoding

http post data encoding

Scheduled Pinned Locked Moved General and Desktop
qnetworkaccessmqurlpost
4 Posts 2 Posters 4.3k Views
  • 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.
  • 4 Offline
    4 Offline
    4j1th
    wrote on last edited by
    #1

    I am trying to create a post request

    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    
        request.setUrl(QUrl(settings.value("app/domainName").toString()));
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    
        QByteArray postData = "json_data="+json.toJson();
    
        QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), this,
                         SLOT(updateToServerReplyFinished(QNetworkReply *)));
    
        manager->post(request, postData);
    

    this works fine but the special character in post data cause error, How can I escape these characters from the post data ?

    Pardon my English
    Thank you.

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      Hi @4j1th,

      First off, do you actually need to be using WWW form encoding? If you control the server, then you might want to skip that encoding, in which case, you'd so something like:

      request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
      QByteArray postData = json.toJson();
      

      But if you do need to use form encoding, you'll want to do something like:

      request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
      QByteArray postData = "json_data="+QUrl::toPercentEncoding(json.toJson());
      

      Cheers.

      4 1 Reply Last reply
      2
      • Paul ColbyP Paul Colby

        Hi @4j1th,

        First off, do you actually need to be using WWW form encoding? If you control the server, then you might want to skip that encoding, in which case, you'd so something like:

        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
        QByteArray postData = json.toJson();
        

        But if you do need to use form encoding, you'll want to do something like:

        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
        QByteArray postData = "json_data="+QUrl::toPercentEncoding(json.toJson());
        

        Cheers.

        4 Offline
        4 Offline
        4j1th
        wrote on last edited by
        #3

        Hi @Paul-Colby , by using the first method how can I access the file (in server), is it works as a file uploading method ?

        Pardon my English
        Thank you.

        1 Reply Last reply
        0
        • Paul ColbyP Offline
          Paul ColbyP Offline
          Paul Colby
          wrote on last edited by
          #4

          @4j1th said:

          Hi @Paul-Colby , by using the first method how can I access the file (in server), is it works as a file uploading method ?

          It depends on your server. What language is your server written with? (Assuming you're POSTing to your own server, and not some third-party API).

          For example, the server was running PHP, you could do something like:

          $data = json_decode(file_get_contents('php://input'), true);
          // save $data to a file somewhere.
          

          In that case, you wouldn't need to use WWW form encoding.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved