REST resource asset: Does it support Dynamic variable in Request URL?


#1

Matrix Version: 5.4.1

In the REST Resource JavaScript asset: I have specified the URL like

https://api-staging.xxxxxxxxx.com/api/v1/credentials/%asset_metadata_cred_id%

The “asset_metadata_xxx” keyword is not resolving ? Does REST Resource asset even support Global keywords ?

I have also tried, %globals_asset_metadata_cred_id%, It works when I print its value in paint layout, but the metadata keyword DOES NOT resolve/work in REST Resource asset.

The API endpoint that I need to query looks like this https://api-staging.xxxxxxxxx.com/api/v1/credentials/{{id}} where “id” is the id of the course.

Then In the paint layout, I am accessing the REST resource using server side JS

<script>
// API call
var course_levels = %globals_asset_contents:33721%; // This is the REST resource
for (var i=0;i<course_levels.length;i++)
{
    print(course_levels[i].name); //This gives an ERROR, as the dynamic keywords in REST resource is not resolving properly.
}
</script>

#2

Can someone please help ? This is really urgent. Many thanks in advance.


(Bart Banda) #3

Yea global keywords are definitely supported in the request URL, but might be the way you are accessing the REST asset via a global keyword itself.

In your paint layout, you could just nest the REST JS asset in (assume you are using the REST JS asset?), and do your JS processing in the REST JS asset itself in the JavaScript processing area instead?

That way you can also test if just accessing the REST JS asset directly (viewing it on the frontend using it’s own URL) works.


#4

Thanks Bart, I need to cherry pick info from the API and print on the page. I can nest the REST JS in paint layout, as you suggested, and also do some post processing (pull just the fields/info I need) in REST JS asset itself (in the JavaScript processing area), BUT …

I have to apply lots of styling to the information extracted from the API … Is adding lots of HTML in the REST JS asset itself (in the JavaScript processing area) a good idea ?


(Bart Banda) #5

Well, you could just do it first to see if it works, and then work your way towards including it via a global keyword like you originally had and see at what point it breaks, that might help us find the bug/missing feature.

But on the other hand, you could try and template it up all in the REST JS area, but using something like a JS templating plugin like http://handlebarsjs.com/. I guess it depends on the foramt of the data you are getting back from the REST Response?


#6

Hi Bart,

 > Well, you could just do it first to see if it works

When I nest the REST JS asset into the paint layout, it works (i.e. the dynamic keyword resolves properly) as expected. But it sorta useless as the whole raw JSON gets printed on the page, and I can’t apply any styling. Although, I can pick the relevant pieces of info from JSON by adding JS in the JavaScript processing area of REST JS resource. But styling issue remain unresolved. I have never worked with handlebarjs. Looks like I need to explore this option now.

Here is where I think it breaks …

When I try to call/nest the REST JS asset using the SSJS, inside a paint layout (as you see above in my sample code), then the dynamic keyword (asset_metadata_xxxx) specified in the REST JS asset (Request URL) is not resolved properly. So it seems seems like calling/nesting REST resource, using the SSJS, inside paint layout, DOES NOT work like the normal nesting does.

I think you can easily verify yourself, by using/calling a public API, an API that looks like

https://restcountries.eu/rest/v2/alpha/{code} where {code} is the country code.

This is a real example https://restcountries.eu/rest/v2/alpha/AU

means an API that DOES NOT use or driven by GET parameters.

In my case, the parameter(id/code) that needs to be passed to the API endpoint is dynamic and needs to be picked up from page metadata.

Thanks Bart …


(Bart Banda) #7

So you could try something like this where the REST asset’s URL is:

https://restcountries.eu/rest/v2/alpha/%nested_get_code%

Then in your Standard page you do something like:

%globals_asset_contents_raw:1234^with_get:code={frontend_asset_metadata_description}%

Where frontend_asset is the asset you are accessing on the frontend.

If you want to pass it to some SSJS, you can:

<script runat="server">
    var data = %globals_asset_contents_raw:1234^with_get:code={frontend_asset_metadata_description}%;
    print(data.name);
</script>

I tested this and it works. However, i’d probably keep the templating of the content within the REST JS asset to keep all the logic there in one place, if you can.

I tested this on 5.4 and it works ok.