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. Sending a Post request with JSON and Image data
Forum Updated to NodeBB v4.3 + New Features

Sending a Post request with JSON and Image data

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 416 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.
  • P Offline
    P Offline
    PrakharP
    wrote on last edited by PrakharP
    #1

    Facing the issue in sending the post request with JSON and image data
    Here is the code

    manager = new QNetworkAccessManager(this);
    QNetworkRequest request;
    QUrl url(location);
    request.setUrl(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
    request.setRawHeader("Authorization", "Basic " + (QString("%1:%2").arg("admin").arg("admin").toLatin1()).toBase64());
    
    /* multi part */
    multiPart = new QHttpMultiPart(manager);
    jsonPart = new QHttpPart();
    jsonPart->setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
    jsonPart->setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"data\""));
    QByteArray data = jsonData.toUtf8(); // conversion of json to byte array
    qDebug() << "QByte Array data: "<< data;
    jsonPart->setBody(data);
    multiPart->append(*jsonPart);
    
    imagePart = new QHttpPart();
    imagePart->setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png"));
    imagePart->setHeader(QNetworkRequest::ContentDispositionHeader,
                        QVariant("form-data; name=\"file\"; filename=\"newImage.jpg\""));
    file = new QFile(jpegFileLocation);
    file->open(QIODevice::ReadOnly);
    qDebug() << "File Open Status: " << file->isOpen();
    imagePart->setBodyDevice(file);
    multiPart->append(*imagePart);
    
    QNetworkReply* reply = manager->post(request, multiPart);
    
    QObject::connect(reply, &QNetworkReply::finished, [=](){
        if(reply->error() == QNetworkReply::NoError){
            QString contents = QString::fromUtf8(reply->readAll());
            qDebug() << "Received Data: " << contents;
        }
        else{
            QString err = reply->errorString();
            qDebug() << "Error:" <<reply->error();
            qDebug() << "Error String:" << err;
        }
        reply->deleteLater();
    });
    

    getting Error in the reply

    Error: QNetworkReply::ProtocolInvalidOperationError

    C 1 Reply Last reply
    0
    • P PrakharP

      Facing the issue in sending the post request with JSON and image data
      Here is the code

      manager = new QNetworkAccessManager(this);
      QNetworkRequest request;
      QUrl url(location);
      request.setUrl(url);
      request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
      request.setRawHeader("Authorization", "Basic " + (QString("%1:%2").arg("admin").arg("admin").toLatin1()).toBase64());
      
      /* multi part */
      multiPart = new QHttpMultiPart(manager);
      jsonPart = new QHttpPart();
      jsonPart->setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
      jsonPart->setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"data\""));
      QByteArray data = jsonData.toUtf8(); // conversion of json to byte array
      qDebug() << "QByte Array data: "<< data;
      jsonPart->setBody(data);
      multiPart->append(*jsonPart);
      
      imagePart = new QHttpPart();
      imagePart->setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png"));
      imagePart->setHeader(QNetworkRequest::ContentDispositionHeader,
                          QVariant("form-data; name=\"file\"; filename=\"newImage.jpg\""));
      file = new QFile(jpegFileLocation);
      file->open(QIODevice::ReadOnly);
      qDebug() << "File Open Status: " << file->isOpen();
      imagePart->setBodyDevice(file);
      multiPart->append(*imagePart);
      
      QNetworkReply* reply = manager->post(request, multiPart);
      
      QObject::connect(reply, &QNetworkReply::finished, [=](){
          if(reply->error() == QNetworkReply::NoError){
              QString contents = QString::fromUtf8(reply->readAll());
              qDebug() << "Received Data: " << contents;
          }
          else{
              QString err = reply->errorString();
              qDebug() << "Error:" <<reply->error();
              qDebug() << "Error String:" << err;
          }
          reply->deleteLater();
      });
      

      getting Error in the reply

      Error: QNetworkReply::ProtocolInvalidOperationError

      C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      @PrakharP On a quick inspection, the main request is not of type "application/json" but probably "multipart/form-data" or "multipart/mixed" , the JSON part is not "text/plain", and the PNG image has an incongruous JPG name but might be OK. Not overly surprising that the server is confused.

      Study the example here more closely.

      P 1 Reply Last reply
      2
      • C ChrisW67

        @PrakharP On a quick inspection, the main request is not of type "application/json" but probably "multipart/form-data" or "multipart/mixed" , the JSON part is not "text/plain", and the PNG image has an incongruous JPG name but might be OK. Not overly surprising that the server is confused.

        Study the example here more closely.

        P Offline
        P Offline
        PrakharP
        wrote on last edited by
        #3

        @ChrisW67 thanks, but even after making the changes, it is showing an Error like

        Error: QNetworkReply::InternalServerError
        Code: 500

        jsulmJ 1 Reply Last reply
        0
        • P PrakharP

          @ChrisW67 thanks, but even after making the changes, it is showing an Error like

          Error: QNetworkReply::InternalServerError
          Code: 500

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @PrakharP Then you should check the server logs to see what happens.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • P Offline
            P Offline
            PrakharP
            wrote on last edited by
            #5

            @ChrisW67 and @jsulm
            from the server got some multipart boundary issue

            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