Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

Content Fragment HTTP API for Create returns 200 content modified response instead of 201

Avatar

Level 2

I'm trying to create a content Fragment with the HTTP API. I have generated the bearer token and I've set it to the request.

 

URL: https://{{bucket}}.adobeaemcloud.com/api/assets/myproject/new-cf

 

Body:

{
    "properties": {
        "description": "New Content Fragment Description",
        "title": "New Content Fragment",
        "name": "new-cf",
        "cq:model": "/conf/myproject/settings/dam/cfm/models/samplemodel",
        "elements": {
            "title": {
                "value": "Test Title for Content Fragment"
            }
        }
    }
}

 

Response:

{
    "class": [
        "core/response"
    ],
    "properties": {
        "path": "/api/assets/myproject/new-name",
        "parentLocation": "/api/assets/myproject.json",
        "referer": "",
        "changes": [],
        "location": "/api/assets/myproject/new-name.json",
        "status.message": "OK",
        "title": "Content modified /api/assets/myproject/new-name",
        "status.code": 200
    }
}

 

I am getting this response instead of 201 content Created. The content fragment is not created in the assets as well.

However, when I create a new Content fragment using console and then use the GET API, I get a proper response.

Any idea on why this is happening? How can we ensure the content fragment gets created?

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 2

The issue was happening due to permission issues. The user using which we generated bearer token for the HTTP API did not have access to create.

View solution in original post

5 Replies

Avatar

Level 8

Hi @SarveshPalani ,

The HTTP API for creating a Content Fragment in AEM returns a 200 status code with a "Content modified" message instead of a 201 status code with a "Content created" message. This is because the API is designed to update an existing Content Fragment if it already exists at the specified path, or create a new one if it does not exist.

In your case, it seems that the Content Fragment was not created because the path specified in the API call already exists. You can verify this by checking if the Content Fragment already exists at the specified path.

To ensure that the Content Fragment gets created, you can modify the API call to use a unique path that does not already exist. For example, you can append a timestamp or a random string to the path to ensure uniqueness.

Here's an example of how you can modify the API call to use a unique path:

URL: https://{{bucket}}.adobeaemcloud.com/api/assets/myproject/new-cf-{{timestamp}}

In this example, we append the current timestamp to the path to ensure uniqueness.

Alternatively, you can use the PUT method instead of the POST method to create the Content Fragment. The PUT method is idempotent, which means that it can be used to create or update a resource. If the resource already exists, it will be updated, and if it does not exist, it will be created.

Here's an example of how you can use the PUT method to create a Content Fragment:

URL: https://{{bucket}}.adobeaemcloud.com/api/assets/myproject/new-cf

Method: PUT

Body:

{
"properties": {
"description": "New Content Fragment Description",
"title": "New Content Fragment",
"name": "new-cf",
"cq:model": "/conf/myproject/settings/dam/cfm/models/samplemodel",
"elements": {
"title": {
"value": "Test Title for Content Fragment"
}
}
}
}

Using the PUT method ensures that the Content Fragment is created if it does not exist, and updated if it already exists.

Avatar

Level 2

@HrishikeshKa , Thank you for replying.

Content fragment does not already exist in the mentioned path. When trying a PUT request with unique path, I get this response:

{
    "class": [
        "core/response"
    ],
    "properties": {
        "path": "/api/assets/myproject/new-cf-1716196378",
        "parentLocation": "/api/assets/myproject.json",
        "referer": "",
        "changes": [],
        "location": "/api/assets/myproject/new-cf-1716196378.json",
        "status.message": "No resource found at /api/assets/myproject/new-cf-1716196378",
        "status.code": 404
    }
}

 

Avatar

Community Advisor

@SarveshPalani 

 

I guess its expected

201 Created

The HTTP 201 Created success status response code indicates that the request has succeeded and has led to the creation of a resource. The new resource, or a description and link to the new resource, is effectively created before the response is sent back and the newly created items are returned in the body of the message, located at either the URL of the request, or at the URL in the value of the Location header.

 

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/201


Aanchal Sikka

Avatar

Level 2

@aanchal-sikka Thanks for your reply.

 

But I'm not getting a 201 created response. As mentioned above, I am getting 200 content modified response. But there is no content in that location as well.

Avatar

Correct answer by
Level 2

The issue was happening due to permission issues. The user using which we generated bearer token for the HTTP API did not have access to create.