2

I've hit a brick wall whilst attempting to update a Contentful entry using a typical http request in Javascript; I receive the error code "VersionMismatch" which, according to the documentation, means:

This error occurs when you're trying to update an existing asset, entry or content type, and you didn't specify the current version of the object or specify an outdated version.

However, I have specified the current version of the entry using the 'X-Contentful-Version' header parameter as per the documentation, and have used the dynamic property value from 'entry.sys.revision' as the parameter value (as well as hardcoding the current version, plus a bunch of different numbers, but I always receive the same error). This post reported the exact same issue, but was seemingly resolved by adding this header parameter.

Here's my current code, that is also using the Contentful API to retrieve entries from my Contentful space, but I'm having to use plain Javascript to put the data back due to specific requirements:

var client = contentful.createClient(
    {
        space: space_id,
        accessToken: client_token
    }
);

client.getEntry(entry_id).then((entry) => entry).then(function(entry) {

    // update values of entry
    entry.fields.title = "Testing";

    // post data to contentful API
    var request = new XMLHttpRequest();
        request.open('PUT', 'https://api.contentful.com/spaces/' + space_id + '/entries/' + entry_id);
        request.setRequestHeader('Authorization', 'Bearer my_access_token');
        request.setRequestHeader('Content-Type', 'application/vnd.contentful.management.v1+json');
        request.setRequestHeader('X-Contentful-Content-Type', entry.sys.contentType.sys.id);

        // setting the 'X-Contentful-Version' header with current/soon to be old version
        request.setRequestHeader('X-Contentful-Version', entry.sys.revision);

    // convert entry object to JSON before sending
    var body = JSON.stringify({fields: entry.fields});

    request.send(body);
});
Community
  • 1
  • 1
mmmoustache
  • 2,273
  • 6
  • 41
  • 62

2 Answers2

4

Contentful developer here.

It looks like you get your content with the Contentful Delivery SDK and then try yo use that data to update content. That will not work. Instead I recommend using the Contentful Management SDK which will take care of all the versioning header for you.

Rouven Weßling
  • 611
  • 4
  • 16
  • 1
    I got here googling how to create content. Leaving this link here in the hope it saves someone my ~45min of googling: https://contentful.github.io/contentful-management.js/contentful-management/5.7.1/ContentfulSpaceAPI.html – TomFuertes May 11 '19 at 18:59
  • 1
    Been at this for 14 hours plus. Sifting, searching, reading the docs, still no closer to being able to update a Contentful number field from a React App. – MadHatter Jun 26 '21 at 11:15
  • 1
    @MadHatter I suggest opening a new question with a code snippet of what you’re trying. I’d be happy to take a look. – Rouven Weßling Jun 27 '21 at 12:23
1

I know its too late to answer here, but I am also facing same issue while doing this process.

So I asked this question and got the answer how we can achieve this without using Contentful-management sdk. My intention is not to suppress this sdk but we should be go to perform operation without it. I am trying to do it via postman but facing issue.

So I just post it and got the answer but understand the problem why I am not able to update the content because of two things

  1. URL (api.contentful.com is the correct url if we are trying to update the content)

  2. access_token (if we are using api.contentful.com which means we have to generate our personal token (management token) in the contentful, we cannot use preview/delivery tokens)

These are the highlighted point which we need to consider while doing update. I posted the link below may be it help someone in future.

Updating entry of Contentful using postman

Kushal Jain
  • 3,029
  • 5
  • 31
  • 48