Hi
First post.
I'm in no way a developer so no business posting in the coding section but I wanted to find a way to add and update my Joomla posts including custom fields using the API. I found it hard to get any clarity on getting custom fields to work but after some back and forth with chatgpt (I know right.. that normally never works haha) I got something working. By adding an article with custom fields first and then using GET I could work backwards from that.
I'm sharing a summary as its what I needed and couldn't find it here.
I know its not rocket science haha... Just hope it helps someone else.
The custom fields in the examples are town, county and top-image. Enjoy.
Here’s a full summary of the POST, PATCH, and GET examples based on what we’ve learned, incorporating text fields and media fields. These examples should cover most scenarios for interacting with Joomla’s API.
---
POST Request
This creates a new article with custom fields.
cURL Command:
curl -X POST "http://domain.com/api/index.php/v1/content/articles" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"title": "New Article with Custom Fields",
"catid": "2",
"introtext": "This is an article created via the API with custom fields.",
"fulltext": "This is the full content of the article.",
"state": "1",
"language": "*",
"town": "Dublin",
"county": "Dublin County",
"top-image": {
"imagefile": "images/joomla_black.png#joomlaImage://local-images/joomla_black.png?width=225&height=50",
"alt_text": "Joomla Logo"
}
}'
---
PATCH Request
This updates an existing article (e.g., article with ID 6) and modifies its fields.
cURL Command:
curl -X PATCH "http://domain.com/api/index.php/v1/content/articles/6" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"title": "Updated Article with Media Field",
"catid": "2",
"introtext": "Updated introtext with media field.",
"fulltext": "Updated full content including media field.",
"state": "1",
"language": "*",
"town": "Dublin",
"county": "Dublin County",
"top-image": {
"imagefile": "images/joomla_black.png#joomlaImage://local-images/joomla_black.png?width=225&height=50",
"alt_text": "Joomla Logo"
}
}'
---
GET Request
This retrieves an existing article and its custom fields (e.g., article with ID 6).
cURL Command:
curl -X GET "http://domain.com/api/index.php/v1/content/articles/6" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json"
Example Response:
{
"links": {
"self": "http://domain.com/api/index.php/v1/content/articles/6"
},
"data": {
"type": "articles",
"id": "6",
"attributes": {
"typeAlias": "com_content.article",
"id": 6,
"title": "Updated Article with Media Field",
"catid": 2,
"introtext": "Updated introtext with media field.",
"fulltext": "Updated full content including media field.",
"state": 1,
"language": "*",
"town": "Dublin",
"county": "Dublin County",
"top-image": {
"imagefile": "images/joomla_black.png#joomlaImage://local-images/joomla_black.png?width=225&height=50",
"alt_text": "Joomla Logo"
}
}
}
}
---
Notes for Each Request Type
#### POST:
Used to create a new article.
Requires title, catid, and optionally introtext, fulltext, state, and language.
Custom fields can be included as direct keys or nested JSON objects (for media).
#### PATCH:
Used to update an existing article.
Only include the fields you want to modify.
Custom fields behave the same as in a POST request.
#### GET:
Retrieves an article by ID.
Displays the full article data, including custom fields.
First post.
I'm in no way a developer so no business posting in the coding section but I wanted to find a way to add and update my Joomla posts including custom fields using the API. I found it hard to get any clarity on getting custom fields to work but after some back and forth with chatgpt (I know right.. that normally never works haha) I got something working. By adding an article with custom fields first and then using GET I could work backwards from that.
I'm sharing a summary as its what I needed and couldn't find it here.
I know its not rocket science haha... Just hope it helps someone else.
The custom fields in the examples are town, county and top-image. Enjoy.
Here’s a full summary of the POST, PATCH, and GET examples based on what we’ve learned, incorporating text fields and media fields. These examples should cover most scenarios for interacting with Joomla’s API.
---
POST Request
This creates a new article with custom fields.
cURL Command:
curl -X POST "http://domain.com/api/index.php/v1/content/articles" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"title": "New Article with Custom Fields",
"catid": "2",
"introtext": "This is an article created via the API with custom fields.",
"fulltext": "This is the full content of the article.",
"state": "1",
"language": "*",
"town": "Dublin",
"county": "Dublin County",
"top-image": {
"imagefile": "images/joomla_black.png#joomlaImage://local-images/joomla_black.png?width=225&height=50",
"alt_text": "Joomla Logo"
}
}'
---
PATCH Request
This updates an existing article (e.g., article with ID 6) and modifies its fields.
cURL Command:
curl -X PATCH "http://domain.com/api/index.php/v1/content/articles/6" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"title": "Updated Article with Media Field",
"catid": "2",
"introtext": "Updated introtext with media field.",
"fulltext": "Updated full content including media field.",
"state": "1",
"language": "*",
"town": "Dublin",
"county": "Dublin County",
"top-image": {
"imagefile": "images/joomla_black.png#joomlaImage://local-images/joomla_black.png?width=225&height=50",
"alt_text": "Joomla Logo"
}
}'
---
GET Request
This retrieves an existing article and its custom fields (e.g., article with ID 6).
cURL Command:
curl -X GET "http://domain.com/api/index.php/v1/content/articles/6" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json"
Example Response:
{
"links": {
"self": "http://domain.com/api/index.php/v1/content/articles/6"
},
"data": {
"type": "articles",
"id": "6",
"attributes": {
"typeAlias": "com_content.article",
"id": 6,
"title": "Updated Article with Media Field",
"catid": 2,
"introtext": "Updated introtext with media field.",
"fulltext": "Updated full content including media field.",
"state": 1,
"language": "*",
"town": "Dublin",
"county": "Dublin County",
"top-image": {
"imagefile": "images/joomla_black.png#joomlaImage://local-images/joomla_black.png?width=225&height=50",
"alt_text": "Joomla Logo"
}
}
}
}
---
Notes for Each Request Type
#### POST:
Used to create a new article.
Requires title, catid, and optionally introtext, fulltext, state, and language.
Custom fields can be included as direct keys or nested JSON objects (for media).
#### PATCH:
Used to update an existing article.
Only include the fields you want to modify.
Custom fields behave the same as in a POST request.
#### GET:
Retrieves an article by ID.
Displays the full article data, including custom fields.
Statistics: Posted by ninjus — Mon Jan 06, 2025 9:26 pm