Important: Please read the Qt Code of Conduct -

QNetworkAccessManager post to url: Error downloading - server replied: BAD REQUEST

  • Hello guys, I'm trying to write an Instagram tool, but I've encountered a problem with QNetworkAccessManager. Even though, the same details ran with curl in CLI it throws some information, while using Qt I always get the error in the title. Am I doing smth wrong in the post script? I followed a dozen examples which were all saying the same thing.

    @curl -F 'client_id=d01483900e3949c194ff6a73910886ba' -F 'client_secret=6d43805a777645ca9d7e96416d4fb83d' -F 'grant_type=authorization_code' -F 'redirect_uri=' -F 'code=9e1aa67677c64b23abe1bb02fc81df96'

    @void MainWindow::_loadWebAuthToken()
    this->_debug("_loadWebAuthToken: TOKEN OBTAIN");

    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(_loadWebAuthTokenReplyFinish(QNetworkReply*)));
    QUrl url;
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    QByteArray postData;
    QNetworkReply *reply = manager->post(request, postData);


    void MainWindow::_loadWebAuthTokenReplyFinish(QNetworkReply *reply)

    if(reply->error() == QNetworkReply::NoError) {
        // Get the http status code
        int v = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
        if (v >= 200 && v < 300) { // Success
            // Here we got the final reply
            QString replyText(reply->readAll());
            this->_debug("{" + replyText + "}");
        } else if (v >= 300 && v < 400) {  // Redirection
            // Get the redirection url
            QUrl newUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
            // Because the redirection url can be relative,
            // we have to use the previous one to resolve it
            newUrl = reply->url().resolved(newUrl);
            QNetworkAccessManager *manager = reply->manager();
            QNetworkRequest redirection(newUrl);
            QNetworkReply *newReply = manager->get(redirection);
            connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(_loadWebAuthTokenReplyFinish(QNetworkReply*)));
            return; // to keep the manager for the next request
    } else {
        // Error
        QString error(reply->errorString());


  • curl "-F" options will cause the request to be sent Content-Type "multipart/form-data": and your Qt code is using application/x-www-form-urlencoded. If Instagram is expecting the multipart format then you should look at QHttpMultiPart

  • It looks like a problem with the URL-encoded code that you're exchanging for a login token. I wrote about authenticating a Qt app with Google SSO, and there I describe how to intercept the login parameters and URL-decode them before Qt continues with the flow.

  • @ihaveajob

    You think after 7 1/2 years OP is still looking for help in this case? ;-)

  • @ihaveajob I see that you have published in several old posts a similar message that seems spam to me, some moderator can analyze this behavior

Log in to reply