Всем добрый день. Начал писать тестовую программу для получения данных из Api судебных приставов. Проект во вложении. При выполнении запроса post возвращается пустое значение. При этом сам запрос копируется в буфер и если его вставить в тестовую версию
https://api-ip.fssp.gov.ru/swagger, он возвращает id задачи. Где мой косяк?
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QFile"
#include "QDebug"
#include "QStandardItemModel"
#include "QJsonObject"
#include "QJsonDocument"
#include "QJsonArray"
#include "QList"
#include "QNetworkAccessManager"
#include "QUrl"
#include "QNetworkRequest"
#include "QNetworkReply"
#include "QSslSocket"
#include "QSslConfiguration"
#include "QDebug"
#include "QClipboard"
#include "QHttpMultiPart"
#include "QHttpPart"
int i=0, z=0;
QJsonDocument massiv_json_doc[10000];
QHttpMultiPart *httpMultiPart;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::curl_post_date(){
z++;
if ((i==0)||(z>i))
return;
QUrl url("https://api-ip.fssp.gov.ru/api/v1.0/search/group");
QNetworkRequest request(url);
request.setRawHeader("Content-Type","application/json");
request.setRawHeader("accept","application/json");
request.setHeader( QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded" );
//QByteArray postDataByteArray;
/*
QFile filetoimport("C:/фссп_JSON"+QString::number(z)+".txt");
QStringList templist;
if ( filetoimport.open(QFile::ReadOnly | QFile::Text) ) {
QString temp="";
temp=filetoimport.readAll();*/
QSslSocket sslSocket;
QSslConfiguration config = sslSocket.sslConfiguration();
config.setProtocol(QSsl::TlsV1_0);
request.setSslConfiguration(config);
QByteArray analiz=massiv_json_doc[z].toJson();
//manager->post(request,massiv_json_doc[z].toJson());
clipboard->setText(analiz);
//QHttpMultiPart *httpMultiPart = new QHttpMultiPart(manager);
httpMultiPart = new QHttpMultiPart(manager);
QHttpPart textPart;
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\""));
textPart.setBody(massiv_json_doc[z].toJson());
httpMultiPart->append(textPart);
//manager->post(request,QString(analiz).toUtf8());
manager->post(request,httpMultiPart);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(read_curl_post1(QNetworkReply*)));
//connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(read_curl_post1(QNetworkReply*)));
//qDebug()<<analiz;
}
void MainWindow::read_curl_post1(QNetworkReply* reply){
QByteArray bts = reply->readAll();
reply->parent()->destroyed();
qDebug()<<bts;
}
void MainWindow::on_pushButton_clicked()
{
manager = new QNetworkAccessManager(this);
QFile filetoimport("C:/фссп.csv");
QStringList templist;
if ( filetoimport.open(QFile::ReadOnly | QFile::Text) ) {
QString temp="";
temp=filetoimport.readAll();
templist=temp.split('\n');
}
i=0;
bool stop=false;
while (!stop) {
QJsonObject data_to_post;
QJsonArray array_to_post;
QJsonDocument doc_to_post;
for (int j=0;((templist.length()!=0)&&(j<50)&&(templist.length()>j)) ;j++) {
QString temp="";
bool lll;
QJsonObject data_to_post2;
QString firstname, lastname, secondname, region, birthdate;
QStringList templist2;
temp=templist.at(j);
templist.removeAt(j);
templist2=temp.split(',');
firstname=templist2.at(0);
lastname=templist2.at(1);
secondname=templist2.at(2);
region=templist2.at(3);
birthdate=templist2.at(4);
data_to_post2["firstname"]=firstname;
data_to_post2["lastname"]=lastname;
data_to_post2["secondname"]=secondname;
data_to_post2["region"]=region.toInt(&lll,10);
data_to_post2["birthdate"]=birthdate;
if (templist.length()==0)
stop=true;
data_to_post["type"]=1;
data_to_post["params"]=data_to_post2;
/*
QJsonObject data_to_post3;
data_to_post3["token"]="rjpvilCeLH7o";
data_to_post3["request"]=data_to_post;
qDebug()<<data_to_post3;
array_to_post.insert(j, data_to_post3);
*/
array_to_post.insert(j, data_to_post);
}
QJsonObject data_to_post3;
data_to_post3["token"]="rjpvilCeLH7o";
data_to_post3["request"]=array_to_post;
//qDebug()<<data_to_post3;
//array_to_post.insert(j, data_to_post3);
i++;
//doc.array()=data_to_post;
/*Рабочий код сохранения файла для консоли*/
/*
QFile filetosave("C:/фссп_JSON"+QString::number(i)+".txt");
if ( filetosave.open(QFile::WriteOnly | QFile::Text) ) {
//QByteArray towrite(data_to_post);
filetosave.write("curl -X POST https://api-ip.fssp.gov.ru/api/v1.0/search/group \\\n-H \"Content-Type: application/json\" \\\n-d @- << EOF\n");
filetosave.write("{\n\"token\":\"rjpvilCeLH7o\",\n\"request\": ");
filetosave.write(QJsonDocument(array_to_post).toJson());
filetosave.write("}\nEOF");
}
filetosave.close();
if (templist.length()==0)
stop=true;
*/
/*Рабочий код сохранения файла для консоли закончился*/
/*
QFile filetosave("C:/фссп_JSON"+QString::number(i)+".txt");
if ( filetosave.open(QFile::WriteOnly | QFile::Text) ) {
//QByteArray towrite(data_to_post);
//filetosave.write("curl -X POST https://api-ip.fssp.gov.ru/api/v1.0/search/group \\\n-H \"Content-Type: application/json\" \\\n-d @- << EOF\n");
filetosave.write("{\n\"token\":\"rjpvilCeLH7o\",\n\"request\": ");
filetosave.write(QJsonDocument(array_to_post).toJson());
//filetosave.write("}\nEOF");
filetosave.write("}");
}
filetosave.close();
if (templist.length()==0)
stop=true;*/
//massiv_json_doc[i]=QJsonDocument(array_to_post);
massiv_json_doc[i]=QJsonDocument(data_to_post3);
}
filetoimport.close();
curl_post_date();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "QNetworkReply"
#include "QClipboard"
#include "QGuiApplication"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
void curl_post_date();
void read_curl_post1(QNetworkReply* reply);
private:
Ui::MainWindow *ui;
QNetworkAccessManager *manager;
QClipboard *clipboard = QGuiApplication::clipboard();
};
#endif // MAINWINDOW_H