Home > Enterprise >  One-drive-api : body must be a string
One-drive-api : body must be a string

Time:09-22

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

CodePudding user response:

You should be able to get this to work by stringifying the body beforehand.

body: JSON.stringify({
  parentReference: {
    id: move_to_id},
  },
  name: new_name,
}),

Looks like its an error from the got library which is what your onedrive-api package is using. Passing in a string should be okay, but if not then there may be a problem with how the onedrive-api package configures the got request

CodePudding user response:

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 });
        });
  • Related