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. JSON encoding for a POST request
Forum Updated to NodeBB v4.3 + New Features

JSON encoding for a POST request

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 2.4k 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.
  • D Offline
    D Offline
    Daniziz
    wrote on 17 Oct 2018, 06:59 last edited by aha_1980
    #1

    Hi all,

    I'm facing a problem with acute characters using Qt.

    I have to post a JSON to an API so I'm using qJsonDocument class in order to write my JSON.

    If I qDebug() my qJsonDocument, i get this:

    createProyect: QJsonDocument({"data":{"name":"Pruebá de acentós","workspace":"5168224827749"}})

    But when I call .toJson() (Acording to documentation, Converts the QJsonDocument to a UTF-8 encoded JSON document) in order to perform a POST, qDebug() gives me this:

    createProyect: qjsondoc.toJson(): "{\n "data": {\n "name": "Prueb\xC3\xA1 de acent\xC3\xB3s",\n "workspace": "5168224827749"\n }\n}\n"

    My acutes turns like "\xC3\xA1" and the server sends me back a server error...

    Any idea why?

    Regards,

    Dani.

    V 1 Reply Last reply 17 Oct 2018, 07:17
    0
    • D Daniziz
      17 Oct 2018, 06:59

      Hi all,

      I'm facing a problem with acute characters using Qt.

      I have to post a JSON to an API so I'm using qJsonDocument class in order to write my JSON.

      If I qDebug() my qJsonDocument, i get this:

      createProyect: QJsonDocument({"data":{"name":"Pruebá de acentós","workspace":"5168224827749"}})

      But when I call .toJson() (Acording to documentation, Converts the QJsonDocument to a UTF-8 encoded JSON document) in order to perform a POST, qDebug() gives me this:

      createProyect: qjsondoc.toJson(): "{\n "data": {\n "name": "Prueb\xC3\xA1 de acent\xC3\xB3s",\n "workspace": "5168224827749"\n }\n}\n"

      My acutes turns like "\xC3\xA1" and the server sends me back a server error...

      Any idea why?

      Regards,

      Dani.

      V Offline
      V Offline
      VRonin
      wrote on 17 Oct 2018, 07:17 last edited by
      #2

      @Daniziz said in JSON encoding for a POST rquest:

      My acutes turns like "\xC3\xA1"

      That's correct, it's the hex codes for the letter's unicode point: http://www.fileformat.info/info/unicode/char/e1/index.htm

      and the server sends me back a server error

      How do you send your string to the server?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • D Offline
        D Offline
        Daniziz
        wrote on 18 Oct 2018, 09:06 last edited by
        #3

        Hi @VRonin

        I just call QNetworkAccessManager POST this way:

        QByteArray jsonUTF8 = jsonDoc.toJson();
        qDebug() << "createTask: POSTING..." << jsonUTF8;
        reply = restclient->post(request, jsonUTF8);
        

        Im starting to think that maybe I must specify enconding in headers petition (But I havent figured it out yet), this is what I do:

        QString header = "Bearer " + access_token;
        request.setRawHeader(QByteArray("Authorization"), QByteArray(header.toLatin1().data()));
        request.setHeader(request.ContentTypeHeader,"Content-Type: application/json");
        

        Thanks for yout help.

        Regards,

        Dani.

        V 1 Reply Last reply 18 Oct 2018, 09:17
        0
        • D Daniziz
          18 Oct 2018, 09:06

          Hi @VRonin

          I just call QNetworkAccessManager POST this way:

          QByteArray jsonUTF8 = jsonDoc.toJson();
          qDebug() << "createTask: POSTING..." << jsonUTF8;
          reply = restclient->post(request, jsonUTF8);
          

          Im starting to think that maybe I must specify enconding in headers petition (But I havent figured it out yet), this is what I do:

          QString header = "Bearer " + access_token;
          request.setRawHeader(QByteArray("Authorization"), QByteArray(header.toLatin1().data()));
          request.setHeader(request.ContentTypeHeader,"Content-Type: application/json");
          

          Thanks for yout help.

          Regards,

          Dani.

          V Offline
          V Offline
          VRonin
          wrote on 18 Oct 2018, 09:17 last edited by VRonin
          #4

          request.setHeader(QNetworkRequest::ContentTypeHeader,QStringLiteral("Content-Type: application/json; charset=utf-8"));

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          5
          • D Offline
            D Offline
            Daniziz
            wrote on 19 Oct 2018, 10:10 last edited by
            #5

            @VRonin OMG, that simple... Thank you very much, it worked!

            I was doing it this way without success...

            request.setRawHeader( "charset", "utf-8" );
            

            Thank you very much!

            Regards,

            Dani.

            1 Reply Last reply
            0

            1/5

            17 Oct 2018, 06:59

            • Login

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