Open Powershell, for example PowerShell 5 or 7 both x86 and x64 bits.
JSON output of all Joomla articles and their settings.
PowerShell 7.4.2: Invoke-RestMethod: Could not match accept header
PowerShell 5.1: Invoke-RestMethod: The remote server returned an error: (406) Not Acceptable
Windows 11 Pro 23H2 with PowerShell 7.4.2.
Joomla 4.4.4.
When using Postman I can get a connection, but via PowerShell it looks like Joomla don;t accept the application/json header.
I use PowerShell to do a lot of automations involving Joomla.
$token = <BEARER_JOOMLA_API_KEY>
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers = @{
'Authorization'='Bearer $token'
'Content-Type'='application/json'
}
$response = Invoke-RestMethod 'https://<DOMAIN_NAME>/api/index.php/v1/content/articles' -Method 'GET' -Headers $headers
$response | ConvertTo-Json
Labels |
Added:
No Code Attached Yet
|
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2024-06-01 13:39:59 |
Closed_By | ⇒ | rdeutz |
This is indeed an PowerShell issue, if someone need it in the feature, this is the correct code to read posts via PowerShell from Joomla 5:
# === Variables ===
$apiUrl = "https://<DOMAIN_NAME>/api/index.php/v1/content/articles" # Change to your site
$apiKey = "<BEARER_JOOMLA_API_KEY>" # Replace with your actual Bearer token
# === Headers ===
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $apiKey")
$headers.Add("Content-Type", "application/json")
$headers.Add("Accept", "application/vnd.api+json")
# === POST Request ===
$response = Invoke-RestMethod -Uri $apiUrl -Method 'GET' -Headers $headers
$response | ConvertTo-Json
And the correct code to post an article:
# === Variables ===
$apiUrl = "https://<DOMAIN_NAME>/api/index.php/v1/content/articles" # Change to your site
$apiKey = "<BEARER_JOOMLA_API_KEY>" # Replace with your actual Bearer token
$title = "Article Title"
$text = "<p>This is the article content.</p>"
$categoryId = 1 # Category ID
$accessLevel = 1 # 1 = Public
$languageCode = "en-US" # Language tag (e.g., "en-US", "nl-NL")
# === Headers ===
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $apiKey")
$headers.Add("Content-Type", "application/json")
$headers.Add("Accept", "application/vnd.api+json")
# === JSON body ===
$body = @{
title = $title
catid = $categoryId
access = $accessLevel
introtext = $text
language = $languageCode
state = 1 # 1 = Published (0 = Unpublished)
} | ConvertTo-Json -Depth 3
# === POST Request ===
try {
$response = Invoke-RestMethod -Uri $apiUrl -Method 'POST' -Headers $headers -Body $body
Write-Host "Article created successfully:"
$response | ConvertTo-Json -Depth 5
} catch {
Write-Host "Error creating article:"
Write-Host $_.Exception.Message
if ($_.ErrorDetails) {
Write-Host $_.ErrorDetails.Message
}
}
This doesn't looks like a bug, please use the forum.joomla.org for such questions