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. How should post data to API that has ContentTypeHeader:"text/plain; charset=utf-8" ?
Forum Updated to NodeBB v4.3 + New Features

How should post data to API that has ContentTypeHeader:"text/plain; charset=utf-8" ?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 2.6k Views 1 Watching
  • 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.
  • zhmhZ Offline
    zhmhZ Offline
    zhmh
    wrote on last edited by
    #1

    I use this link to find API ContentTypeHeader

    this line:

    qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString();
    

    print :

    "text/plain; charset=utf-8"
    

    so for post data I do like:

    QNetworkRequest request(QUrl("http://site.com/api/PtData/"));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain; charset=utf-8");
     QByteArray const data("145,2018/02/16,19:17:23");
     QNetworkAccessManager nam;
     QNetworkReply *reply = nam.post(request, data);
    

    or

    QNetworkRequest request(QUrl(QStringLiteral("http://site.com/api/PtData/")));
     request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("text/plain; charset=utf-8"));
    QString text(QStringLiteral("145,2018/02/16,19:17:23"));
    QByteArray const data = text.toUtf8();
    QNetworkAccessManager nam;
    QNetworkReply *reply = nam.post(request, data);
    

    But the server replied:

    Unsupported Media Type
    

    where is the problem?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      From the looks of it, you are trying to send the wrong kind of data to that end point. You should check its documentation to see what it expects.

      The fact that it replies with text has nothing to do with what is valid to send to it.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      I zhmhZ 2 Replies Last reply
      2
      • SGaistS SGaist

        Hi,

        From the looks of it, you are trying to send the wrong kind of data to that end point. You should check its documentation to see what it expects.

        The fact that it replies with text has nothing to do with what is valid to send to it.

        I Offline
        I Offline
        isan
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          From the looks of it, you are trying to send the wrong kind of data to that end point. You should check its documentation to see what it expects.

          The fact that it replies with text has nothing to do with what is valid to send to it.

          zhmhZ Offline
          zhmhZ Offline
          zhmh
          wrote on last edited by zhmh
          #4

          @SGaist am I get the correctly contentTypeheader With that link ?

          1 Reply Last reply
          0
          • zhmhZ Offline
            zhmhZ Offline
            zhmh
            wrote on last edited by
            #5

            And am I use contentTypeHeader correctly?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Did you check what the endpoint you are sending requests to requires ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #7

                Can you show us an example of valid request to your API? Maybe using curl?

                P.S.
                Qt tutorials from https://www.bogotobogo.com should be avoided, they are of very low quality and sometimes encourage toxic patterns in code.

                "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

                zhmhZ 1 Reply Last reply
                2
                • VRoninV VRonin

                  Can you show us an example of valid request to your API? Maybe using curl?

                  P.S.
                  Qt tutorials from https://www.bogotobogo.com should be avoided, they are of very low quality and sometimes encourage toxic patterns in code.

                  zhmhZ Offline
                  zhmhZ Offline
                  zhmh
                  wrote on last edited by zhmh
                  #8

                  @SGaist @VRonin I send post request with postman in google chrome
                  I set header Content-Type:"application/json"
                  in body section I choose raw
                  and data:"122,2019/02/21,10:54:31"
                  data is successfully posted to the API and I can see it on the URL

                  0_1550778498718_Untitled.jpg

                  I change my code :

                   QNetworkRequest request(QUrl("http://site.com/api/PtData/"));
                  request.setRawHeader("Content-Type", "application/json");
                     QByteArray data("102,2018/02/16,19:17:23");
                      QNetworkAccessManager nam;
                      QNetworkReply *reply = nam.post(request, data);
                  

                  I do not get any errors, but when I check the URL, the data is not post(set)on it

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Except that what you send is not valid JSON.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    zhmhZ 1 Reply Last reply
                    2
                    • SGaistS SGaist

                      Except that what you send is not valid JSON.

                      zhmhZ Offline
                      zhmhZ Offline
                      zhmh
                      wrote on last edited by zhmh
                      #10

                      @SGaist but I post the same format data that I use in postman

                      POST /api/PatData/ HTTP/1.1
                      Host: site.com
                      Content-Type: application/json
                      Cache-Control: no-cache
                      Postman-Token: c31beda7-12bf-d5b8-4945-88895d0727a0
                      
                      "102,2019/02/21,23:31:21"
                      
                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        You should check the data sent using postman.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        zhmhZ 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          You should check the data sent using postman.

                          zhmhZ Offline
                          zhmhZ Offline
                          zhmh
                          wrote on last edited by zhmh
                          #12

                          I checked the C code in postman the data format must be :

                          QByteArray data("\"185,2019/02/22,10:59:21\"");
                          

                          Finally it's post successfuly :-/

                          1 Reply Last reply
                          0

                          • Login

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