4

I've read what little I can find about J4 Web Services, but cannot find a working example or get my code to work. The code below should work by all accounts, but I get forbidden on my local machine and not found on our web server. There is no mention of how to be set up to use the API or much else to go on.

Has anyone else tried one of these API calls?

$data = new stdClass();
$data->alias = "my-article";
$data->articletext = "My text";
$data->catid = 11;
$data->language = "*";
$data->metadesc = "metadesc";
$data->metakey = "metakey";
$data->title = "Here's an article";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '  = 'http://localhost/myproject/api/index.php/v1/content/article -d';);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

REturn: (I'm not knowingly using Watchguard anywhere on EC2 or Localhost)

<html>

<body>
<h3> Request denied by WatchGuard HTTP proxy. </h3>
<b> Reason: </b> request protocol version denied line='POST /api/index.php/v1/content/article -d HTTP/1.1\x0d\x0a' <br>
<hr size="1" noshade>
<b> Method: </b> POST <br>
<b> Host: </b> 168.156.127.75 <br>
<b> Path: </b> /api/index.php/v1/content/article <br>
<hr size="1" noshade>

Update: Further attempts yield only errors, so apparently there is far more to getting this to work than just a CURL request. However, the question itself was answered, so thank you for that :). I don't know enough to ask without being too general, so will keep an eye out for someone that does.

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
    <title>400 Bad Request</title>
    </head><body>
    <h1>Bad Request</h1>
    <p>Your browser sent a request that this server could not understand.<br />
</p>
3
  • 1
    It should be content/articles, not content/article.
    – Sharky
    Sep 21 at 6:00
  • 1
    docs.joomla.org/J4.x:Joomla_Core_APIs has a list of useful resources as well as the full list of API calls. Sep 22 at 3:27
  • content/article is correct if you're trying to POST content to the database. content/articles is the GET command to get a list of articles, content/article will get a single article when GET is used. Sep 22 at 3:31
4

You can download the full API collection here:

https://github.com/alexandreelise/j4x-api-collection thanks to Alexandre Elise.

This will import directly into Postman which will enable you to use the API simply by adding your key and URL to the collection.

To create a key you need to:

  1. Make sure the plugins are enabled. Joomla API Plugins

  2. Make sure your user has a token This is the tab

Hit save.

API key after saving

Copy the key and use that with your Bearer Token.

If you use Postman and click on the code icon it gives you the cURL example.
You can also use the dropdown to change it to any other language too. How to find the cURL example

curl --location --request POST 'https://lovelaserandskin.co.uk/dev/api/index.php/v1/content/article' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer c2hhMjU2OjEyMToxZjFmNmMzNDRkMzkyMDhjYTUxMDVjNjRmMTJlODljMWM4NGJkODhmOTQ3ZmZhZTMyZTY5NzU3NzE0NTZkZmJi' \
--data-raw '{'\''alias'\'': '\''my-article'\'','\''articletext'\'': '\''My text'\'','\''catid'\'': 64,'\''language'\'': '\''*'\'','\''metadesc'\'': '\'''\'','\''metakey'\'': '\'''\'','\''title'\'': '\''Here'\''s an article'\''}'

Add your key to the collection

  1. Click the 3 dots on the collection and press edit Press Edit on the collection

  2. Set the key Set the key

  3. Edit the base_url Edit the base_url

  4. Hit close which will save it

  5. Now revisit the collection and click any of them and hit send. It should give you a response and you can work back from there to use cURL.

Hope that helps.

Some video resources:

If the docs aren't clear... please edit them and make them better.

Another useful resource packed with other resources

1

You should include a user token with a bearer authentication header. You can get the token from your super admin account (Joomla API Token tab), given that the relevant plugin (User - Joomla API Token) is enabled.

For the shake of simplicity you can try a curl tool like postman where all the headers can be added from a GUI.

Also as mentioned by Sharky the endpoint is: content/articles

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.