c++ - Qt http bad request on google -
i'm trying create url shortener app using google's url shortener api (https://developers.google.com/url-shortener/v1/getting_started#shorten). problem bad request. here error: error: error downloading https://www.googleapis.com/urlshortener/v1/url - server replied: bad request
what did wrong? here 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); } }
you have send post data in json format.
to make code work, replace
qbytearray postdata; postdata.append("longurl"); postdata.append(longurl);
with this
qbytearray postdata; postdata.append("{\"longurl\": \""+longurl+"\"}");
Comments
Post a Comment