使用Qt的RESTful API请求

时间:2018-08-13 06:19:26

标签: c++ qt httprequest hl7-fhir hapi-fhir

我正在尝试从此link中找到示例以供使用。

GETDELETE方法可以使用,但PUTPOST却存在一些问题。我得到服务器答复:错误的请求。使用qDebug()时出现此错误:

  

QNetworkReply :: NetworkError(ProtocolInvalidOperationError)

对于 httprequestworker.cpp ,我已将请求类型更改为:

request_content.append("Content-Type: application/fhir+json");

这是我的功能,用于从计算机获取JSON文件的内容并执行对服务器请求的全部输入:

void MainWindow::on_pushButton_clicked()
{
    QString url_str = "http://hapi.fhir.org/baseDstu3/Patient/4705560";

    HttpRequestInput input(url_str, "PUT");

    QString settings;
    QFile file;

    file.setFileName("C:/Users/Lauri/Desktop/FHIR/Omia testeja/themostsimplepatientJSON.json");
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    settings = file.readAll();
    file.close();

    settings.remove(QRegExp("[\\n]"));
    qDebug() << settings;
    settings.toUtf8();
    input.add_var("key1", settings);


    HttpRequestWorker *worker = new HttpRequestWorker(this);
    connect(worker, SIGNAL(on_execution_finished(HttpRequestWorker*)), this, SLOT(handle_result(HttpRequestWorker*)));
    worker->execute(&input);
}

这是我要上传的简单JSON:

{
  "resourceType": "Patient",
  "text": {
    "status": "generated",
    "div": "<div xmlns='http://www.w3.org/1999/xhtml'>This is a test patient<a name='mm'/></div>"
  },
  "name": [
    {
      "use": "usual",
      "prefix": [
        "Mr"
      ],
      "given": [
        "Teppo",
        "Testi"
      ],
      "family": "Testinen"
    }
  ],
  "telecom": [
    {
      "value": "123456789",
      "system": "phone",
      "use": "home"
    }
  ],
  "gender": "male",
  "birthDate": "2018-08-21"
}

JSON文件应该是正确的,因为我已经能够使用邮差和其他工具POSTPUT。有什么明显的我想念的东西吗?

邮递员拍摄的照片:

PUT Headers

PUT Body

POST Headers

POST Body

使用qDebug()可以看到读取JSON成功。我一直在尝试为ProtocolInvalidOperationError找到解决方案,但没有成功。

1 个答案:

答案 0 :(得分:0)

HttpRequestWorker不支持json的发送,因此在示例中将不再使用它。在这种情况下,我将直接使用QNetworkAccessManager

对于PUT,您必须在.json中添加ID,以便您可以修改文件或通过代码进行处理,在这种情况下,请使用第二种情况:

输入:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QNetworkAccessManager>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();
    void onManagerFinished(QNetworkReply *reply);

private:
    Ui::MainWindow *ui;
    QNetworkAccessManager manager;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
#include <QNetworkReply>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(&manager, &QNetworkAccessManager::finished, this, &MainWindow::onManagerFinished);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QNetworkRequest request(QUrl("http://hapi.fhir.org/baseDstu3/Patient/4705560"));
    request.setRawHeader("Content-Type", "application/fhir+json");
    QFile file("/path/of/themostsimplepatientJSON.json");
    if(file.open(QIODevice::ReadOnly)){
        QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
        QJsonObject obj = doc.object();
        obj["id"] = "4705560"; // add ID
        doc.setObject(obj);
        manager.put(request, doc.toJson());
    }
}

void MainWindow::onManagerFinished(QNetworkReply *reply)
{
    qDebug()<< reply->readAll();
}

输出:

"{\n  \"resourceType\": \"OperationOutcome\",\n  \"text\": {\n    \"status\": \"generated\",\n    \"div\": \"<div xmlns=\\\"http://www.w3.org/1999/xhtml\\\"><h1>Operation Outcome</h1><table border=\\\"0\\\"><tr><td style=\\\"font-weight: bold;\\\">INFORMATION</td><td>[]</td><td><pre>Successfully created resource \\\"Patient/4705560/_history/7\\\" in 5ms</pre></td>\\n\\t\\t\\t\\t\\t\\n\\t\\t\\t\\t\\n\\t\\t\\t</tr>\\n\\t\\t\\t<tr>\\n\\t\\t\\t\\t<td style=\\\"font-weight: bold;\\\">INFORMATION</td>\\n\\t\\t\\t\\t<td>[]</td>\\n\\t\\t\\t\\t\\n\\t\\t\\t\\t\\t\\n\\t\\t\\t\\t\\t\\n\\t\\t\\t\\t\\t\\t<td><pre>No issues detected during validation</pre></td>\\n\\t\\t\\t\\t\\t\\n\\t\\t\\t\\t\\n\\t\\t\\t</tr>\\n\\t\\t</table>\\n\\t</div>\"\n  },\n  \"issue\": [\n    {\n      \"severity\": \"information\",\n      \"code\": \"informational\",\n      \"diagnostics\": \"Successfully created resource \\\"Patient/4705560/_history/7\\\" in 5ms\"\n    },\n    {\n      \"severity\": \"information\",\n      \"code\": \"informational\",\n      \"diagnostics\": \"No issues detected during validation\"\n    }\n  ]\n}"

POST:

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QMessageBox>
#include <QNetworkReply>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(&manager, &QNetworkAccessManager::finished, this, &MainWindow::onManagerFinished);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QNetworkRequest request(QUrl("http://hapi.fhir.org/baseDstu3/Patient")); // without ID
    request.setRawHeader("Content-Type", "application/fhir+json");
    QFile file("/path/of/themostsimplepatientJSON.json");
    if(file.open(QIODevice::ReadOnly)){
        QByteArray ba = file.readAll();
        manager.post(request, ba);
    }
}

void MainWindow::onManagerFinished(QNetworkReply *reply)
{
    qDebug()<< reply->readAll();
}

输出:

"{\n  \"resourceType\": \"OperationOutcome\",\n  \"text\": {\n    \"status\": \"generated\",\n    \"div\": \"<div xmlns=\\\"http://www.w3.org/1999/xhtml\\\"><h1>Operation Outcome</h1><table border=\\\"0\\\"><tr><td style=\\\"font-weight: bold;\\\">INFORMATION</td><td>[]</td><td><pre>Successfully created resource \\\"Patient/4728838/_history/1\\\" in 3ms</pre></td>\\n\\t\\t\\t\\t\\t\\n\\t\\t\\t\\t\\n\\t\\t\\t</tr>\\n\\t\\t\\t<tr>\\n\\t\\t\\t\\t<td style=\\\"font-weight: bold;\\\">INFORMATION</td>\\n\\t\\t\\t\\t<td>[]</td>\\n\\t\\t\\t\\t\\n\\t\\t\\t\\t\\t\\n\\t\\t\\t\\t\\t\\n\\t\\t\\t\\t\\t\\t<td><pre>No issues detected during validation</pre></td>\\n\\t\\t\\t\\t\\t\\n\\t\\t\\t\\t\\n\\t\\t\\t</tr>\\n\\t\\t</table>\\n\\t</div>\"\n  },\n  \"issue\": [\n    {\n      \"severity\": \"information\",\n      \"code\": \"informational\",\n      \"diagnostics\": \"Successfully created resource \\\"Patient/4728838/_history/1\\\" in 3ms\"\n    },\n    {\n      \"severity\": \"information\",\n      \"code\": \"informational\",\n      \"diagnostics\": \"No issues detected during validation\"\n    }\n  ]\n}"

完整的示例可以在下面的link

中找到