post to http [SOLVED]



  • How to post some request command and file to a link?
    I have a link "http://192.168.100.1/".
    tha form have id='Ssid' value.
    My code :
    QByteArray mydata ;

        QNetworkRequest request(QUrl("http://192.168.100.1/"));
        mydata.append("Ssid=QQ");
        mydata.append(file.readAll());
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
        manager->post(request,mydata);
    

    But, I can't recevie mydata from the host?!



  • Hi,

    have you checked if you get some errors?

    QNetworkReply *reply = manager->post(request, mydata);
    
    connect(reply, SIGNAL(error(QNetworkReply::NetworkError code)), this, SLOT(handleError(QNetworkReply::NetworkError code)));
    
    void MyClass::handleError(QNetworkReply::NetworkError code)
    {
    ....
    }
    


  • @mcosta

    "Ssid" is my command type for request

    and I find that "mydata.append("Ssid=QQ");" is illegal for my Qt 5.4. ="=

    I must declare QUrlQuery query.

    and the statement should be "qrery.addQueryItem( "Ssid", "QQ")"......

    and url.setQuery(query).

    Is it legal statement before?
    Or I could use "mydata.append("Ssid=QQ");" being my request command variable in some way?



  • Hi,

    the URL must be encoded ("application/x-www-form-urlencoded") to be sent correctly using HTTP (addQueryItem does it for you).

    I think it's better to use QUrlQuery in order to handle correctly encodig


  • Moderators

    Hi @mkdh I don't think there is any problem with append method.
    Is the url and the port correct ? By default it will hit port 80.
    Also is there a POST handler on server side ?



  • Hi @p3c0 ,
    I don't think there is any problem with append method too.

    But, In my opinion, if you want to get the request command type or value, you must use setQuery method.

    Or could I use "mydata.append("Ssid=QQ");" being my request command variable in other way?


  • Moderators

    @mkdh Did you check url and port ?

    could I use "mydata.append("Ssid=QQ");" being my request command variable in other way?

    I didn't quite understand this.
    What is the problem exactly ? The data ("SSid=QQ") is not received on server side ?

    I just executed your code and using python on server side. Well, just modified the url, port and IP address. It works perfectly. Here's the python code:

    //myserver.py
    import web
    
    urls = (
        "/get", "get",
        "/post", "post"
    )
    app = web.application(urls, globals())
    
    class get:
        def GET(self):
            return "get"
    
    class post:
        def POST(self):
            data = web.data()
            print data // <- This prints the data which we POST'ed from Qt
            return "post"
    
    if __name__ == "__main__":
        app.run()
    

    This runs by default on http://0.0.0.0:8080/ ( to execute just run python myserver.py ) and on Qt side

    QByteArray mydata ;
    QNetworkRequest request(QUrl("http://0.0.0.0:8080/post"));
    mydata.append("Ssid=QQ");
    mydata.append(file.readAll());
    

    You can try running it if you are familiar with python. As @mcosta mentioned earlier did you get any errors ?



  • Hi @p3c0 ,
    I use Qt 5.4 for IOS/android. And install my program to my Android Phone.
    My partner use IAR to detect my request on server side.
    He told me that he don't receive my "Ssid" command from request link.
    But I had solved this problem by using setQuery method.
    Thank for you reply.^^


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.