Search code examples
node.jsonedrive

One-drive-api : body must be a string


I'm using the library one-drive-api and trying to implement an enpoint to meve item from folder to another here's my code

exports.moveItem = (req, res) => {
  let { item_id, move_to_id, new_name } = req.body;
  oneDriveAPI.items
    .customEndpoint({
      accessToken: accessToken,
      URL: "/me/drive/items/"+item_id,
      method: 'PATCH',
      body: {
        parentReference: {
          id: move_to_id},
        },
        name: new_name,
      },
    })
    .then((r) => {
      res.status(200).send({ drives: r });
    })
    .catch((e) => {
      res.status(500).send({ error: e.message });
    });
};

And I'm getting this error

 {
    "error": "The `body` option must be a stream.Readable, string or Buffer"
}

Here's the reference to doc from Microsoft : link and here's the library I'm using : npm


Solution

  • You can try this:

    let { item_id, move_to_id, new_name } = req.body;
          let body_req = { parentReference: { id: move_to_id } };
          let stringified = JSON.stringify(body_req);
          let b = Buffer.from(stringified);
          oneDriveAPI.items
            .customEndpoint({
              accessToken: accessToken,
              url: `/me/drive/items/${item_id}`,
              method: 'PATCH',
              body: b,
            })
            .then((r) => {
              res.status(200).send({ moved: r });
            })
            .catch((e) => {
              res.status(500).send({ error: e.message });
            });