'Contentful API returning 'version mismatch' on entry update

I'm attempting to do the following with the Content Management API for Contentful:

  1. Get an entry (entry1)
  2. Find another entry (entry2) using data from a field in entry1
  3. Update entry1 with data from entry2

My code looks like this:

client.getSpace("xxxxxxxx").then(function(space){
  space.getEntries({
    "content_type": "xxxxxxxx",
    "sys.id": "2KEZYJOgDSeQMCQIE0Oo88",
    "limit": 1
  }).then(function(places){

    //search for relevant category entry
    space.getEntries({
      "content_type": contentType.category,
      "sys.id": places[0].fields.category["en-GB"],
      "limit": 1
    }).then(function(category){

      //update place object
      places[0].fields.categoryNew = {
        "en-GB": [ 
          { sys: { type: "Link", linkType: "Entry", id: category[0].sys.id } } 
        ]
      };        

      //update place
      request({
        method: 'PUT',
        url: 'https://api.contentful.com/spaces/xxxxxxxx/entries/' + places[0].sys.id,
        headers: {
          'Authorization': 'Bearer xxxxxxxx',
          'Content-Type': 'application/vnd.contentful.management.v1+json',
          'X-Contentful-Content-Type': 'xxxxxxxx'
        },
        body: JSON.stringify({fields:places[0].fields})
      }, function (error, response, body) {
        console.log(body);
      });

    });


  });
});

Steps 1 and 2 work fine but the final step, updating the original entry, keeps returning the following error:

Response: {
  "sys": {
    "type": "Error",
    "id": "VersionMismatch"
  },
  "requestId": "content-api:2PSSF6RtpSs2YyaaisK2wc"
}

How do I stop this happening? I've tried everything I can think of including manually updating the sys.version number, but when updating it seems to ignore any sys data I provide.



Solution 1:[1]

Refer to https://www.contentful.com/developers/docs/references/content-management-api/#/introduction/updating-and-version-locking

You need to pass the version as a header parameter called "X-Contentful-Version" with the PUT request.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kristoffer