sp-request icon indicating copy to clipboard operation
sp-request copied to clipboard

Possible incorrect metadata returns 400 Bad Request when create new list item

Open heroneto opened this issue 6 years ago • 0 comments

https://github.com/s-KaiNet/sp-request/blob/9f3b27dcc25cf3fe0deb266717fc38051d39909b/test/integration/integration.spec.ts#L168

Hi, I'm using this module, and he is amazing, but in my tests i'm getting Response Code 400 when create a new item in list....

After some hours and thousands of Chrome tabs in Stackoverflow, i did not find any reference about possible incorrect _metadata type format.

Then, i test parsing _metadata like this: body: { "__metadata": { "type": "SP.List" }, "Title": "New Item" },

And error 400 no more occours....

If you wan't to try or put this in your documentation can be very helpfull for income users.

Here's my full code for consultant:

const spClient = sprequest.create({
  username,
  password
})

spClient.requestDigest(url).then(digest => {
  return spClient.post(`${url}/_api/web/lists/getByTitle('nodejsapp')/items	`, {
    body: {
      "__metadata": {
        "type": "SP.List"
      },
      "Title": "New Item"
    },
    headers: {
      'X-RequestDigest': digest
    }  
  })
}).then(function () {
  console.log('item just created!!!');
}, function (err) {
    // if (err.message.indexOf('-2130575342') === -1) {
    //     console.log('Hmmmmm something went wrong');
    //     console.log(err);
    //     return;
    // }
    // console.log('list already exists!!!');
    
})
    .catch(function (err) {
    console.log('Hmmmmm something went wrong');
    console.log(err);
});

heroneto avatar Nov 11 '20 15:11 heroneto