Qt http bad request on google



  • I'm trying to create an url shortener app using google's url shortener api (https://developers.google.com/url-shortener/v1/getting_started#shorten). The problem is that I get a bad request.
    Here is the error:
    Error: Error downloading https://www.googleapis.com/urlshortener/v1/url - server replied: Bad Request

    What did I do wrong?
    Here is the code:
    @

    void MainWindow::ppp(QString longurl)
    {
    QNetworkAccessManager* manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));

    QUrl url = QUrl("https://www.googleapis.com/urlshortener/v1/url");
    
    QNetworkRequest request(url);
    request.setHeader(request.ContentTypeHeader,"application/json");
    
    QByteArray postData;
    postData.append("longUrl=");
    postData.append(longurl);
    
    
    manager->post(request,postData);
    

    }

    void MainWindow::replyFinished(QNetworkReply *reply)
    {
    if(reply->error() != QNetworkReply::NoError)
    {
    ui->textBrowser->setText("Error: " + reply->errorString());
    }
    else
    {
    QByteArray responseData = reply->readAll();
    QString qstr(responseData);
    ui->textBrowser->setText(qstr);
    }
    }
    @



  • could it be because google is expecting a json document like
    @{"longUrl": "http://www.google.com/"}@
    and you are sending name-value pairs ?


Log in to reply
 

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