3

I'm trying to update a Contentful entry using Postman.

What I did:

  • In Contentful space, I created a test post to play with.
  • Went to Settings - API Keys - Content management tokens and generated a Personal access token
  • Created a GET request in Postman, passing space ID, master environment, and ID of the test post:

https://cdn.contentful.com/spaces/{spaceID}i/environments/master/entries?sys.id={postID} I also sent authorisation header with content delivery token.

enter image description here

The GET request goes successfully and I'm able to copy the JSON object-response.

{
  "sys": {
  "type": "Array"
},
  "total": 1,
  "skip": 0,
  "limit": 100,
  "items": [
  {
    "metadata": {
      "tags": []
    },
    "sys": {
      "space": {
        "sys": {
          "type": "Link",
          "linkType": "Space",
          "id": "d9r4mg123x4v"
        }
      },
      "id": "2Fwow39hxxx1bvMkjpsyV9",
      "type": "Entry",
      "createdAt": "2021-11-10T14:00:11.935Z",
      "updatedAt": "2021-11-10T14:06:51.393Z",
      "environment": {
        "sys": {
          "id": "master",
          "type": "Link",
          "linkType": "Environment"
        }
      },
      "revision": 3,
      "contentType": {
        "sys": {
          "type": "Link",
          "linkType": "ContentType",
          "id": "hotelInfo"
        }
      },
      "locale": "en-US"
    },
    "fields": {
      "name": "Test entry",
      "slug": "test-entry",
      "address": "Lviv",
      "cityName": "Lviv",
      "phone": "+380931231212",
      "coordinates": {
        "lon": -115.302,
        "lat": 36.18709
      },
      "dog": "100",
      "cat": "100",
      "delivery": "100",
      "photo": [
        {
          "sys": {
            "type": "Link",
            "linkType": "Asset",
            "id": "2hSnYhQDJzU99NvlsYdk3k"
          }
        }
      ],
      "additionalInfo": {
        "data": {},
        "content": [
          {
            "data": {},
            "content": [
              {
                "data": {},
                "marks": [],
                "value": "Test",
                "nodeType": "text"
              }
            ],
            "nodeType": "paragraph"
          }
        ],
        "nodeType": "document"
      },
      "featuredHotel": true,
      "phoneClicks": 1
    }
  }
],
  "includes": {
  "Asset": [
    {
      "metadata": {
        "tags": []
      },
      "sys": {
        "space": {
          "sys": {
            "type": "Link",
            "linkType": "Space",
            "id": "d9r4mg123x4v"
          }
        },
        "id": "2hSnYhQDJzU99NvlsYdk3k",
        "type": "Asset",
        "createdAt": "2021-11-10T13:59:59.954Z",
        "updatedAt": "2021-11-10T13:59:59.954Z",
        "environment": {
          "sys": {
            "id": "master",
            "type": "Link",
            "linkType": "Environment"
          }
        },
        "revision": 1,
        "locale": "en-US"
      },
      "fields": {
        "title": "JS",
        "description": "Lorem Ipsum",
        "file": {
          "url": "//images.ctfassets.net/d9r4mg123x4v/2hSnYhQDJzU99NvlsYdk3k/6fbabc7be7f4b28dc8b7deadd9892205/JS.png",
          "details": {
            "size": 23078,
            "image": {
              "width": 1024,
              "height": 1024
            }
          },
          "fileName": "JS.png",
          "contentType": "image/png"
        }
      }
    }
  ]
}
}

Now I want to create PUT request to send the updated JSON to the Contentful. I paste the JSON I got as a response from GET request. I change one of the values:

"name": "Test entry" to "name": "Test entry 123"

I send PUT request to https://api.contentful.com/spaces/{spaceID}/environments/master/entries/{postID}

enter image description here

The Authorisation header contains the Personal access token I generated before. The X-Contentful-Version header contains the version of the post, can be found in post details

enter image description here

When I send this request, I get JSON response with an empty "fields": {}

{
  "metadata": {
  "tags": []
},
  "sys": {
  "space": {
    "sys": {
      "type": "Link",
        "linkType": "Space",
        "id": "d9r4mg123x4v"
    }
  },
  "id": "2Fwow39hxxx1bvMkjpsyV9",
    "type": "Entry",
    "createdAt": "2021-11-10T13:57:10.882Z",
    "updatedAt": "2021-11-11T10:58:39.480Z",
    "environment": {
    "sys": {
      "id": "master",
        "type": "Link",
        "linkType": "Environment"
    }
  },
  "publishedVersion": 13,
    "publishedAt": "2021-11-10T14:06:51.393Z",
    "firstPublishedAt": "2021-11-10T14:00:11.935Z",
    "createdBy": {
    "sys": {
      "type": "Link",
        "linkType": "User",
        "id": "4123123123zOn3MkhuVB"
    }
  },
  "updatedBy": {
    "sys": {
      "type": "Link",
        "linkType": "User",
        "id": "4123123123zOn3MkhuVB"
    }
  },
  "publishedCounter": 3,
    "version": 23,
    "publishedBy": {
    "sys": {
      "type": "Link",
        "linkType": "User",
        "id": "4123123123zOn3MkhuVB"
    }
  },
  "contentType": {
    "sys": {
      "type": "Link",
        "linkType": "ContentType",
        "id": "hotelInfo"
    }
  }
},
  "fields": {}
}

And in Contentful Admin area, all the fields of the post become empty.

enter image description here

Contentful documentation says:

Contentful doesn't merge changes made to content, so when updating content, you need to send the entire body of an entry. If you update content with a subset of properties, you will lose all existing properties not included in that update.

You should always update resources in the following order:

  • Fetch current resource.
  • Make changes to the current resource.
  • Update the resource by passing the changed resource along with current version number.

This way no unseen changes are overridden and unexpected conflicts are unlikely to occur.

Note: You can't update any of the sys property fields, including sys.id.

...so, I guess, I'm doing everything right - taking the post, editing data and sending updated post back. I tried editing my JSON data to send it without sys fields, but no luck. I'm stuck, anyone has any ideas what should I proceed with?

user3297120
  • 61
  • 1
  • 3
  • 1
    Contentful DevRel here! Thanks so much for your detailed explanation. I wonder whether the payloads are different on the CDA and the CMA. Perhaps try getting the initial payload from the CMA (api.contentful.com) and resending that back to the same API URL? – whitep4nth3r Nov 11 '21 at 14:49
  • @whitep4nth3r right you are! Yeah, I thought I had to use CDA and avoid using CMA for fetching data. – user3297120 Nov 11 '21 at 16:07

1 Answers1

2

Thanks to @whitep4nth3r I was able to solve the problem. I needed to GET data from the same source I'm trying to PUT it to. The Authorisation header needed to be replaced with the Personal access token used for PUT request.

enter image description here

user3297120
  • 61
  • 1
  • 3