QNetworkReply behavior againt local site with authentication - qt

i'm using QNetworkReply in order to issue a simple GET request to my router interface. Basically if the post data is empty i issue a GET otherwise i will issue a POST. Let's stick with the GET
QString url=ui->lineEdit_url->text();
QString paras=ui->pTextEdit_paras->toPlainText();
qDebug()<< "paras" << paras;
QByteArray post_data;
post_data.append(paras);
QNetworkRequest request = QNetworkRequest(QUrl(url));
request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
if(post_data.isEmpty())
{
//nam->head(request);
nam->get(request);
}
else
{
nam->post(request,post_data);
}
now with
...
connect(nam,
SIGNAL(finished(QNetworkReply*)),
this,
SLOT(finished(QNetworkReply*)));
...
void HttppostWindow::finished(QNetworkReply *reply)
{
if(reply->error() == QNetworkReply::NoError)
{
ui->textEdit_result->setText(QObject::tr(reply->readAll()));
}
else
{
ui->textEdit_result->setPlainText(reply->errorString());
}
}
i show the answer in the ui.
Right now the local interface asks for a login and a pw. The problem is that the retrieved text with the GET command is the one that the interface would show if the user would have insterted a wrong password (autentication failed please try again and so on).
Moreover with the code nam->head(request) i should be able to retrieve the header, but the content of replyAll is empty.
Any ideas?

After nam->head(request) you don't need to use reply->readAll().
Instead of this you should use methods like:
QByteArray rawHeader(const QByteArray &headerName) const;
QList<QByteArray> rawHeaderList() const;
const QList<RawHeaderPair> &rawHeaderPairs() const;
With this methods you can view content of your head request.
Also your sample code has memory leak. You should delete the reply whis reply->deleteLater() inside your finished slot.

Related

Google assistant gRPC call hung

Server doesn't reply when a GRPC call is made to "embeddedassistant.googleapis.com". I see the request being received at google server end when I check the Google API web interface.
I set the request with proper configuration (when I set wrong configuration I do get error message from server). Is there anything I'm missing here?
std::string Converse(const std::string& user) {
AudioInConfig audio_in_config;
audio_in_config.set_encoding(google::assistant::embedded::v1alpha1::AudioInConfig_Encoding_FLAC);
audio_in_config.set_sample_rate_hertz(16000);
AudioOutConfig audio_out_config;
audio_out_config.set_encoding(google::assistant::embedded::v1alpha1::AudioOutConfig_Encoding_MP3);
audio_out_config.set_sample_rate_hertz(16000);
audio_out_config.set_volume_percentage(50);
ConverseState converse_state;
const char * conversation_state = "a";
converse_state.set_conversation_state(conversation_state);
ConverseConfig config;
config.set_allocated_audio_in_config(&audio_in_config);
config.set_allocated_audio_out_config(&audio_out_config);
config.set_allocated_converse_state(&converse_state);
ConverseRequest request;
request.set_allocated_config(&config);
ConverseResponse reply;
ClientContext context;
auto status = stub_->Converse(&context, request, &reply);
config.release_audio_in_config();
config.release_audio_out_config();
config.release_converse_state();
request.release_config();
// Act upon its status.
if (status.ok()) {
return reply.result().conversation_state();
} else {
std::cout << "Error: " << status.error_code() << ": " << status.error_message()
<< std::endl;
return "RPC failed";
}
return "";
}
Why have you set the conversation_state to "a". It should be in bytes or empty. You also need to send an audio data captured depending upon the situation. You can do that by including the ALSA sound API in C++ in your code.
The conversation_state value returned in the prior ConverseResponse. Omit (do not set the field) if there was no prior ConverseResponse. If there was a prior ConverseResponse, do not omit this field; doing so will end that conversation (and this new request will start a new conversation).
You can see from here:-
https://developers.google.com/assistant/sdk/reference/rpc/google.assistant.embedded.v1alpha1

How to pass several parameters when uploading image to imgur

I'm trying to make a function that uploads a picture to imgur and the function is working in the form below but differently then I was expecting.
bool ImgurUploader::upload( QImage image )
{
QByteArray byteArray;
QBuffer buffer(&byteArray);
image.save(&buffer, "PNG");
QByteArray params;
params.append(byteArray.toBase64());
QNetworkRequest request;
request.setUrl(QUrl("https://api.imgur.com/3/image"));
request.setRawHeader("Content-Type", "application/application/x-www-form-urlencoded");
request.setRawHeader("Authorization", "Client-ID 16d41e28a3ba71e");
mAccessManager->post(request, params);
}
I was expecting and trying at first to pass the image param in form like this:
params.append("image=");
params.append(byteArray.toBase64());
But when I do I get an "400 Bad Request" and the error is "Invalid URL".
How can I send several params? Am I using the wrong approach?
You can have a look at their Android Upload Example.
If you look at the upload request here, you can see that parameters are not sent in the body of the POST request (as you are trying to do). Instead, they are queries that are appended to the URL (this can be done in Qt using QUrlQuery), the image is sent in the body of the request and there is no need to encode it using base64 (this is better as it can save some network traffic).
Here is how your upload function should look like:
bool ImgurUploader::upload(QImage image, QString title, QString description)
{
QByteArray byteArray;
QBuffer buffer(&byteArray);
image.save(&buffer, "PNG");
QUrlQuery urlQuery;
urlQuery.addQueryItem("title", title);
urlQuery.addQueryItem("description", description);
QNetworkRequest request;
QUrl url("https://api.imgur.com/3/image");
url.setQuery(urlQuery);
request.setUrl(url);
request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/application/x-www-form-urlencoded");
request.setRawHeader("Authorization", "Client-ID 16d41e28a3ba71e");
mAccessManager->post(request, byteArray);
}

Get http code request

U use QNetworkRequest to send post request. How can I get HTTP code of request? I send some request to server, on server I can see my request, but i have to check http code which server will return to application.
QNetworkRequest can not be used without QNetworkAccessManager that's responsible for making the actual request to the web server. Each request done by QNetworkAccessManager instance returns QNetworkReply where you should look for the status code from the server. It's located inside the QNetworkReply instance headers.
The request is asynchronous so it can be catch when signal is triggered.
Easiest example would be:
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("http://qt-project.org")));
Then in the slot implementation:
void replyFinished(QNetworkReply *resp){
QVariant status_code = resp->attribute(QNetworkRequest::HttpStatusCodeAttribute);
status_code.is_valid(){
// Print or catch the status code
QString status = status_code.toString(); // or status_code.toInt();
qDebug() << status;
}
}
Have a look on the official documentation. It explains all in details.
QNetworkRequest
QNetworkAccessManager

Nginx filtering message before proxy

I have been digging into creating a custom filtering module written in C that can process post messages. Read the json payload, make a decision whether to proxy the message or return a 204 to the client.
I am having trouble reading the payload of the message, I have read through quite a few of the modules but am missing something. I have tried quite a few ways to parse the payload but can't seem to figure out how to just simply print it.
I have read through the echo module, redis module, Evan Miller's documentation, etc.
I know that I need to read the full message before I can process the payload but the callback has got me confused. I don't really understand the callback in ngx_http_read_client_request_body. I have tried to read the code but there really is almost no documentation in the code.
In the code below I have tried multiple things to get the request body, I tried reading r->request_body->buf, bufs once the full body was read. When I try to print the request body, I get segfault. I think the echo module would have been the closest to what I wanted to do but I got confused in the ngx_http_echo_wev_handler on how it was processing the request.
static ngx_int_t ngx_http_ortb_handler(ngx_http_request_t *r) {
ngx_int_t rc;
ngx_flag_t read_body;
if (r->method != NGX_HTTP_POST) {
read_body = 1;
}
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Received Post");
if (read_body) {
rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "return code %ld", rc);
// What do I need to do here to read the body.
} else {
r->main->count++;
rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK) {
return rc;
}
ngx_http_upstream_init(r);
}
return NGX_DECLINED;
}

QtWebkit: How to check HTTP status code?

I'm writing a thumbnail generator as per an example in the QtWebkit documentation. I would like to avoid screenshots of error pages such as 404 not found or 503 Internal server error.
However, the QWebPage::loadFinished() signal is always emitted with ok = true even when the page gives an HTTP error. Is there a way in QtWebkit to check the HTTP status code on a response?
Turns out you need to monitor the QNetworkAccessManager associated with your QWebPage and wait for a finished(...) signal. You can then inspect the HTTP response and check its status code by asking for the QNetworkRequest::HttpStatusCodeAttribute attribute.
It's better explained in code:
void MyClass::initWebPage()
{
myQWebPage = new QWebPage(this);
connect(
myQWebPage->networkAccessManager(), SIGNAL(finished(QNetworkReply *)),
this, SLOT(httpResponseFinished(QNetworkReply *))
);
}
void MyClass::httpResponseFinished(QNetworkReply * reply)
{
switch (reply->error())
{
case QNetworkReply::NoError:
// No error
return;
case QNetworkReply::ContentNotFoundError:
// 404 Not found
failedUrl = reply->request.url();
httpStatus = reply->attribute(
QNetworkRequest::HttpStatusCodeAttribute).toInt();
httpStatusMessage = reply->attribute(
QNetworkRequest::HttpReasonPhraseAttribute).toByteArray();
break;
}
}
There are more NetworkErrors to choose from, e.g. for TCP errors or HTTP 401.
This is what I'm using in a porting project. It checks the reply and decides to start backing off making request or not. The backing off part is in progress but I left the comments in.
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
Q_CHECK_PTR(reply);
QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
if (!statusCode.isNull() && statusCode.toInt() >= 400){
//INVALID_SERVER_RESPONSE_BACKOFF;
qDebug() << "server returned invalid response." << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
return;
}else if (!statusCode.isNull() && statusCode.toInt() != 200){
//INVALID_SERVER_RESPONSE_NOBACKOFF;
qDebug() << "server returned invalid response." << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
return;
}

Resources