带有Microsoft图javascript sdk的多部分HTTP请求

时间:2018-07-03 00:39:26

标签: javascript microsoft-graph onenote

我正在尝试使用Microsoft Graph JavaScript SDK在OneNote中创建带有图像的页面,OneNote需要多部分请求。我已经创建了一个FormData对象,其中包含我要发送的所有数据。

当我自己将请求发送如下时,请求会通过:

var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader("Authorization", "Bearer" + token);
xhr.onreadystatechange = function() {
  //Call a function when the state changes
  if (xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
    // Request finished. Do processing here.
  } else {
    // handle case
  }
};
// dataToSend = FormData object containing data
// (as Blobs), including the page HTML in a
// "Presentation" part as specified
xhr.send(dataToSend);

但是,由于我使用的是Graph SDK来执行所有其他请求,因此我想知道是否还可以使用SDK进行多部分请求。到目前为止,这是我尝试过的:

this.client
  .api(pagesURL)
  .version("beta")
  .header("Content-Type", "text/html")
  .post(dataToSend);

在Fiddler中调查请求表明,请求正文包含[object, Object],而不是格式化为多部分请求的数据。十分感谢使用SDK /指南确定如何将FormData对象正确地放入请求中的任何帮助!

2 个答案:

答案 0 :(得分:0)

我相信这是您想要的:

-Powershell Command Add-Type -AssemblyName System.Speech
$Speaker = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
$Speaker.Speak('oh my god, I can now talk; it''s amazing!')

此代码段改编自SDK本身使用的多部分单元测试。您可以在https://github.com/microsoftgraph/msgraph-sdk-javascript/blob/dev/spec/types/OneNote.ts

找到该测试

答案 1 :(得分:0)

将microsoft-graph-client更新到最新版本,然后尝试类似的操作。

    const HTMLPageContent = 
    `<!DOCTYPE html>
    <html>
        <head>
            <title>A page with rendered images</title>
        </head>
        <body>
            <p>Here is an image uploaded as <b>binary data</b>:</p>
            <img src="name:imageBlock1" alt="an image on the page" />
        </body>
    </html>`;

    let sectionId = "<Your_OneNote_Page_Section_Id>";
    let formData = new FormData();
    let htmlBlob = new Blob([HTMLPageContent], {
        type: "text/html"
    });
    formData.append("Presentation", htmlBlob);
    formData.append("imageBlock1", file);
    client
        .api(`/me/onenote/sections/${sectionId}/pages`)
        .post(formData)
        .then((json) => {
            console.log(json);
            return Promise.resolve();
        });