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. QNetworkAccessManager to access cloudant REST API
Forum Updated to NodeBB v4.3 + New Features

QNetworkAccessManager to access cloudant REST API

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.8k 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.
  • I Offline
    I Offline
    iaguirre
    wrote on last edited by iaguirre
    #1

    I am trying to make a post to a cloudant REST API , but I obtain this reply message:
    Satus: "400"
    Answer: "{"error":"bad_request","reason":"Referer header required."}\n"
    The same request with a REST client, obtains the desired answer from Cloudant.

    This is the code where the request is constructed:
    QNetworkAccessManager m_networkManager;
    QNetworkRequest m_request;
    QNetworkReply m_reply;
    .
    .
    .
    QByteArray jsonPost;
    jsonPost = createJsonDocum(cmd);
    QUrl url = QUrl(qsurl);
    if (url.isValid())
    {
    m_request.setUrl(url);
    m_request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json; charset=utf-8"));
    m_request.setHeader(QNetworkRequest::ContentLengthHeader, QByteArray::number(jsonPost.size()));
    m_reply = m_networkManager->post(m_request,jsonPost);
    }
    connect(m_networkManager, SIGNAL(finished(QNetworkReply
    )),
    this, SLOT(replyReceived(QNetworkReply
    )));

    And the slot for the reply
    void CloudantRESTClient::replyReceived(QNetworkReply * reply)
    {
    QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    // Print or catch the status code
    QString status = status_code.toString(); // or status_code.toInt();
    qDebug() << "Satus: "<< status;
    QByteArray answer = reply->readAll();
    qDebug() << "Answer: "<<answer;
    }

    And this is the json of the post request:
    {
    "fields": [
    "timestamp",
    "data.data.data"
    ],
    "selector": {
    "deviceId": "BE7A000000002417"
    }
    }

    Please, any help would be appreciated.

    Idoia

    1 Reply Last reply
    0
    • Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by
      #2

      @iaguirre from the error message you posted, it looks like your request is missing the "Referer header" the REST server is expecting.
      You can use the setRawHeader() method from QNetworkRequest, since that header is not defined in the QNetworkRequest::KnownHeaders enum.
      For the right value to set, you stated another client is working

      The same request with a REST client, obtains the desired answer from Cloudant.

      so you may need to set that client in "debug" mode or do a network traffic capture to see what it is actually sending for the Referer header.

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • I Offline
        I Offline
        iaguirre
        wrote on last edited by
        #3

        Using insomnia as REST Client I don't see Referer header in the Post Request.
        On the other hand, I can't see Qtś Http request because all is encryted :-)

        Any idea?

        Pablo J. RoginaP 1 Reply Last reply
        0
        • I iaguirre

          Using insomnia as REST Client I don't see Referer header in the Post Request.
          On the other hand, I can't see Qtś Http request because all is encryted :-)

          Any idea?

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #4

          @iaguirre said in QNetworkAccessManager to access cloudant REST API:

          Using insomnia as REST Client I don't see Referer header in the Post Request.
          On the other hand, I can't see Qtś Http request because all is encryted :-)

          @iaguirre think about your reply, why could be that with the same request in both clients (Insomnia and Qt) you can see headers for one and not for the other?

          I assume you're using the same network traffic inspection tool for both.
          It's strange that if it's "encrypted" on Qt it's not the same for insomnia...

          Any chances that you post the actual request and JSON data to have a better idea? Please be sure to not disclose usernames/passwords/API keys

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          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