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

Qt http bad request on google

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.1k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    ddacot
    wrote on last edited by
    #1

    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);
    }
    }
    @

    1 Reply Last reply
    0
    • clogwogC Offline
      clogwogC Offline
      clogwog
      wrote on last edited by
      #2

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

      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