Skip to content

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

@heroneto

Description

@heroneto

'__metadata': { 'type': `SP.Data.${listTitle}ListItem` },

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions