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.
  • Z Offline
    Z Offline
    zhmh
    wrote on 20 Feb 2019, 21:35 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 20 Feb 2019, 21:40 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 Z 2 Replies Last reply 21 Feb 2019, 03:54
      2
      • S SGaist
        20 Feb 2019, 21:40

        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 21 Feb 2019, 03:54 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • S SGaist
          20 Feb 2019, 21:40

          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.

          Z Offline
          Z Offline
          zhmh
          wrote on 21 Feb 2019, 03:58 last edited by zhmh
          #4

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

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            zhmh
            wrote on 21 Feb 2019, 14:53 last edited by
            #5

            And am I use contentTypeHeader correctly?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 21 Feb 2019, 16:24 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
              • V Offline
                V Offline
                VRonin
                wrote on 21 Feb 2019, 16:49 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

                Z 1 Reply Last reply 21 Feb 2019, 20:00
                2
                • V VRonin
                  21 Feb 2019, 16:49

                  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.

                  Z Offline
                  Z Offline
                  zhmh
                  wrote on 21 Feb 2019, 20:00 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
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 21 Feb 2019, 21:24 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

                    Z 1 Reply Last reply 21 Feb 2019, 21:39
                    2
                    • S SGaist
                      21 Feb 2019, 21:24

                      Except that what you send is not valid JSON.

                      Z Offline
                      Z Offline
                      zhmh
                      wrote on 21 Feb 2019, 21:39 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
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 21 Feb 2019, 22:36 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

                        Z 1 Reply Last reply 22 Feb 2019, 07:11
                        0
                        • S SGaist
                          21 Feb 2019, 22:36

                          You should check the data sent using postman.

                          Z Offline
                          Z Offline
                          zhmh
                          wrote on 22 Feb 2019, 07:11 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

                          1/12

                          20 Feb 2019, 21:35

                          • Login

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