如何向包含多部分 HTTP POST 的 SendGrid web api 发出请求

How to make a request to SendGrid web api which includes a multipart HTTP POST

我知道有类似的问题,但我仍然无法让它工作或理解我应该做什么。

我正在用 C++ 编写应用程序并使用 QtCreator。我正在尝试使用 SendGrid 网络 api 发送一封附有 html 文件的电子邮件。到目前为止,我已经能够发送电子邮件,但我无法将文件附加到它(该文件是本地文件)。文档说:

The file contents must be part of the multipart HTTP POST. Ex: files[file1.jpg]=file1.jpg&files[file2.pdf]=file2.pdf'

我不太确定这是什么意思,所以我研究了什么是多部分 http post 并看到了示例,但我看不出如何让它与我正在做的事情一起工作。 到目前为止,我需要做的就是调用 post,输入请求的 link 和 post 数据:

api_user=your_sendgrid_username&api_key=your_sendgrid_password&to=destination@example.com&toname=Destination&subject=Example_Subject&text=testingtextbody&from=info@domain.com

我看不出多部分 HTTP post 会在哪里出现以及它如何替换当前的 post 数据,我看到了设置 Header 和 Body 在多部分 HTTP post 中,但我看不出其中任何一个与我正在做的事情或我应该用它做什么有关。

有人可以给我一个示例,说明如何将本地文件附加到 post 对 Qt C++ 中 SendGrid 的调用

更新: 这是我对贾斯汀斯蒂尔的说明的解释:

 char speechMk = '"';
QString boundary = "----------------------------123456789abc";
    QByteArray data(QString("--" + boundary + "\r\n").toLatin1());
    data.append(QString("Content-Disposition: form-data; name=" + QString(speechMk) + "api_user" + QString(speechMk) + "\r\n").toLatin1());
       data.append("myuser");
       data.append("\r\n");

       data.append("--" + boundary + "\r\n");
       data.append("Content-Disposition: form-data; name=" + QString(speechMk) + "api_key" + QString(speechMk) + "\r\n" );
       data.append("mypassword");
       data.append("\r\n");

       data.append("--" + boundary + "\r\n");
       data.append("Content-Disposition: form-data; name=" + QString(speechMk) + "to" + QString(speechMk) + "\r\n");
       data.append("myemail@hotmail.co.uk");
       data.append("\r\n");

        data.append("--" + boundary + "\r\n");
       data.append("Content-Disposition: form-data; name=" + QString(speechMk) + "toname" + QString(speechMk) + "\r\n");
       data.append( "Destination");
       data.append("\r\n");

       data.append("--" + boundary + "\r\n");
       data.append("Content-Disposition: form-data; name=" + QString(speechMk) + "subject" + QString(speechMk) + "\r\n");
       data.append("eSubject");
       data.append("\r\n");

       data.append("--" + boundary + "\r\n");
       data.append("Content-Disposition: form-data; name=" + QString(speechMk) + "text" + QString(speechMk) + "\r\n");
       data.append("eBody");
       data.append("\r\n");

       data.append("--" + boundary + "\r\n");
      data.append("Content-Disposition: form-data; name=" + QString(speechMk) + "from" + QString(speechMk) + "\r\n");
      data.append("noreply@randomdomain.com");
      data.append("\r\n");

      data.append("--" + boundary + "\r\n");
      data.append("Content-Disposition: form-data; name=" +  QString(speechMk) + "files[1]" + QString(speechMk) + "; filename=" + QString(speechMk) + "TodayRoute.html" + QString(speechMk) + "\r\n");

      data.append("Content-Type: application/octet-stream");
      data.append("\r\n");
      QFile file1(path);
      if (!file1.open(QFile::ReadOnly)){
          qDebug() << "could not open";
      } else {
          data.append(file1.readAll().toBase64() + "\r\n");
      }
      data.append("--" + boundary + "\r\n");

    file1.close();

    QNetworkAccessManager theMan;

    QNetworkRequest req = QNetworkRequest(QUrl("https://api.sendgrid.com/api/mail.send.json"));
    req.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("multipart/form-data; boundary=" + boundary));
    theMan.post(req,data);

这是我之前的(下图),它会发送一封带有附件的电子邮件,但附件不会包含我想要的内容。

 QByteArray data(QString("api_user=myuser&").toLatin1());
        data.append("api_key=mypassword&");
        data.append("to=myemail@hotmail.co.uk&");
        data.append("toname=Destination&");
        data.append("files[TodayRoute.html]=");
        QFile file(path);
            if (!file.open(QIODevice::ReadOnly)){
                qDebug() << "QFile Error: File not found!";
            //    return data;
            } else { qDebug() << "File found, proceed as planned"; }
        data.append(file.readAll());

        data.append("&subject=Example_Subject&");
        data.append("text=testingtextbody&");
        data.append("from=noreply@domainrandom.com");



           QUrl mResultsURL = QUrl("https://api.sendgrid.com/api/mail.send.json");
           QNetworkAccessManager mNetworkManager;
           QNetworkRequest request(mResultsURL); //our server with php-script
          mNetworkManager.post(request,data);
        file.close(); 

我使用 C# 尝试了这个,了解到您需要在 Content-Disposition: 行和值之间有一个空行“\r\n”。我相信您在文件的 Content-Type: 行之后还需要一个。最后一个边界以'--'结尾。

    char speechMk = '"';
    QString boundary = "----------------------------123456789abc";

    QByteArray data(QString("--" + boundary + "\r\n").toLatin1());

    data.append(QString("Content-Disposition: form-data; name=" + QString(speechMk) + "api_user" + QString(speechMk) + "\r\n").toLatin1());
    data.append("\r\n");
    data.append("myuser");
    data.append("\r\n");

    data.append("--" + boundary + "\r\n");
    data.append("Content-Disposition: form-data; name=" + QString(speechMk) + "api_key" + QString(speechMk) + "\r\n" );
    data.append("\r\n");
    data.append("mypassword");
    data.append("\r\n");

    data.append("--" + boundary + "\r\n");
    data.append("Content-Disposition: form-data; name=" + QString(speechMk) + "to" + QString(speechMk) + "\r\n");
    data.append("\r\n");
    data.append("myemail@hotmail.co.uk");
    data.append("\r\n");

    data.append("--" + boundary + "\r\n");
    data.append("Content-Disposition: form-data; name=" + QString(speechMk) + "toname" + QString(speechMk) + "\r\n");
    data.append("\r\n");
    data.append( "Destination");
    data.append("\r\n");

    data.append("--" + boundary + "\r\n");
    data.append("Content-Disposition: form-data; name=" + QString(speechMk) + "subject" + QString(speechMk) + "\r\n");
    data.append("\r\n");
    data.append("eSubject");
    data.append("\r\n");

    data.append("--" + boundary + "\r\n");
    data.append("Content-Disposition: form-data; name=" + QString(speechMk) + "text" + QString(speechMk) + "\r\n");
    data.append("\r\n");
    data.append("eBody");
    data.append("\r\n");

    data.append("--" + boundary + "\r\n");
    data.append("Content-Disposition: form-data; name=" + QString(speechMk) + "from" + QString(speechMk) + "\r\n");
    data.append("\r\n");
    data.append("noreply@randomdomain.com");
    data.append("\r\n");

    data.append("--" + boundary + "\r\n");
    data.append("Content-Disposition: form-data; name=" +  QString(speechMk) + "files[1]" + QString(speechMk) + "; filename=" + QString(speechMk) + "TodayRoute.html" + QString(speechMk) + "\r\n");

    data.append("Content-Type: application/octet-stream");
    data.append("\r\n");
    data.append("\r\n");

    QFile file1(path);
    if (!file1.open(QFile::ReadOnly)){
        qDebug() << "could not open";
    } else {
        data.append(file1.readAll().toBase64() + "\r\n");
    }
    data.append("--" + boundary + "--\r\n");

    file1.close();

    QNetworkAccessManager theMan;

    QNetworkRequest req = QNetworkRequest(QUrl("https://api.sendgrid.com/api/mail.send.json"));
    req.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("multipart/form-data; boundary=" + boundary));
    theMan.post(req,data);