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 to post request on Qt?
Forum Updated to NodeBB v4.3 + New Features

How to post request on Qt?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 2.3k 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.
  • R Offline
    R Offline
    Roberrt
    wrote on last edited by Roberrt
    #1

    I'm making a post request with the following code:

    QNetworkAccessManager* manager = new QNetworkAccessManager(this);
    
    QNetworkRequest request;
    request.setUrl(QUrl("http://127.0.0.1:3001/api/users/login"));
    request.setHeader(QNetworkRequest::ContentTypeHeader, 
    	"application/x-www-form-urlencoded");
    
    QByteArray postData;
    postData.append(R"({"email": ")")
    	.append(p->ui->email->toPlainText().toStdString())
    	.append("\", ")
    	.append(R"("password": ")")
    	.append(p->ui->password->toPlainText().toStdString())
    	.append("\"}");
    
    qDebug() << "postData: " << postData;
    
    QNetworkReply* reply = manager->post(request, postData);
    
    // Connect the finished signal to a slot to handle the response
    connect(reply, &QNetworkReply::finished, [reply, this]
    {
    	if(reply->error() == QNetworkReply::NoError)
    	{
    		QByteArray response = reply->readAll();
    		qDebug() << "response: " << response;
    
    	}
    	else // handle error
    	{
    	         qDebug() << reply->errorString();
    	}
    
    	reply->deleteLater();
    });
    

    the value printed by qDebug() << "postData: " << postData; is :

    postData:  "{\"email\": \"123\", \"password\": \"123\"}"
    

    However, I'm receiving a string literal on the javascript side:

    500cbe17-1b21-4a84-9037-3a739cfa6814-image.png

    email, and the password is undefined.

    When i manually request using reqbin

    {"email": "123", "password": "123"}
    

    The request I receive:

    930fe163-a74b-451b-bd65-23eb8f59e1bc-image.png
    email and password are 123.

    I also tried:

    	QByteArray postData;
    	postData.append("{\"email\": \"")
    		.append(p->ui->email->toPlainText().toStdString())
    		.append("\", ")
    		.append("\"password\": \"")
    		.append(p->ui->password->toPlainText().toStdString())
    		.append("\"}");
    

    But receive the same kind of request, what i'm doing wrong?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Roberrt
      wrote on last edited by
      #2
      QNetworkRequest request;
      	request.setUrl(QUrl("http://127.0.0.1:3001/api/users/login"));
      	request.setHeader(QNetworkRequest::ContentTypeHeader, 
      		"application/json");
      
      	QJsonObject json;
      	json.insert("email", "test");
      	json.insert("password", "123");
      
      	QJsonDocument doc(json);
      	QByteArray postData = doc.toJson();
      
      	qDebug() << "postData: " << postData;
      
      	QNetworkReply* reply = manager->post(request, postData);
      

      this worked.

      Is QNetworkRequest as 'secure' than lib curl? it does use OpenSSL by default or do I need to do something to activate it?

      Asking because I have no experience in this topic.

      jsulmJ 1 Reply Last reply
      0
      • R Roberrt
        QNetworkRequest request;
        	request.setUrl(QUrl("http://127.0.0.1:3001/api/users/login"));
        	request.setHeader(QNetworkRequest::ContentTypeHeader, 
        		"application/json");
        
        	QJsonObject json;
        	json.insert("email", "test");
        	json.insert("password", "123");
        
        	QJsonDocument doc(json);
        	QByteArray postData = doc.toJson();
        
        	qDebug() << "postData: " << postData;
        
        	QNetworkReply* reply = manager->post(request, postData);
        

        this worked.

        Is QNetworkRequest as 'secure' than lib curl? it does use OpenSSL by default or do I need to do something to activate it?

        Asking because I have no experience in this topic.

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

        @Roberrt said in How to post request on Qt?:

        it does use OpenSSL by default

        It uses SSL for https URLs. http does not require SSL.
        To be able to access https URLs in your app you will also need to deploy OpenSSL libraries with your app. Qt already provides them.

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

        1 Reply Last reply
        3

        • Login

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