Modx REST APi questions

I set
header(‘Access-Control-Allow-Origin: *’);
header(‘Allow: GET’);
in rest/resources.php but still I cant get the variables such as id, pagetitle, content anything on my app…
They return nothing. I can see json on browser, can POST via Postman but I cant get it on my app.
Is there anything I have to do with Access-Control-Allow-Origin ?
I think I am missing something here.


what I am missing ? I cant get data on my app. If I remove Access control from resources.php then in my app, it returns Angular’s HttpErrorResponse

If the response contains the correct data in the JSON then that means the server-side (MODX) is working as expected.
It sounds like something isn’t correct on the front-end. Perhaps you’re accessing the response data incorrectly.

1 Like

I am calling just like they call in wordpress rest api in a tutorial and i am expecting same response with modx rest api. I dont knkw where im doing wrong.

Actually i want to move my 15 wordpress sitea to Modx and most of them has rest api use now. So i am preparing first with Modx rest api.

@lkfranklin when will you make next video of this series “MODX React CMP and Restful API setup” ???
Please include React Native tutorial with Modx rest api call in next video. I still has some doubts and issues as you can see in my previous posts here. :sweat_smile:

It could be that Wordpress returns it’s results in a different structure.
As you can see in your example, MODX returns the resource data under the “results” parent.
You would need to adapt your code so it’s targeting the correct location within the JSON.

1 Like

I need to learn more. Where and how can I learn these stuffs? I m curious about it…

Please mind your tone - you’re coming across as very demanding of people donating their precious time for your education. :wink:

Wow thank you so much this was very helpful indeed!

Im asking where to learn… Not asking for them to teach me
:joy:

Are you sure you’re not asking to be taught? It sure looks that way to me.

Liam’s a great teacher obviously and his videos are very useful so I get you’re excited about him doing more of them, but please just mind the tone. Try not using 3 question marks when only one is enough for example, that would make you seem a lot less impatient.

1 Like

Ok sir. I expected much more from the community than it can offer to users. The documentation is not very broad and in detail thats why i was here for help. If you think that it is too much then you can close the topic.

It is better for me to stick with wordpress than wasting my time with unfinished documentations and asking too much from other members to teach me how modx rest api works.

Now I get it why Modx is awesome but not very popular and not getting storm of new users. You broke my heart. :slightly_smiling_face:

You can close this thread now. Nothing more left here. :wink:
@markh

Are you for real?

I’ve personally spent around 2-3 hours explaining the REST API stuff to you, trawling through source code getting you answers, example code and references, precisely because I know the documentation about this topic (all of which I’ve written too, by the way) doesn’t cover all the possibilities and that you ask questions others may also have, which are great to have answers available for.

The other people in this thread, too, have gone out of their way to explain you things and to clarify how things work.

What I ask in return is that you try to be patient with the people who have invested all that time in you, but that’s too much and actually breaks your heart? Seriously? You’ve broken mine by being so ungrateful towards a community rallying to help you.

I just dropped message so that someone reply when they got time. I never messaged anyone in personal to help me. So its clear that i am in rush but i just want to learn. Not telling anyone to give me exact codes. I just want to learn how things works here in rest api.

I appreciated your work and still appreciate and will always thank you for you time and help. But you are telling that i am asking too much. That is not good thing.

I already clear my vision that i am looking for solution that help others too. I want to help other members too with rest api. So i asked about it where to or how to learn these things because I dont want to create a new topic for every little problem. I want to learn so I can help myself and others.

Dont get me wrong. I always appreciated your work and time for me. :heart::blush::+1:

Also thanked other active members who helped me.

Just wanted to be a part of helpers by growing my knowledge about Modx. :slight_smile::slightly_smiling_face:

As @digitalpenguin said it sounds like it’s a problem with your front end code, rather than with MODX. Postman is a third party app, just like whatever you are developing, so if it works there it should work for your app too. As digital said be mindful that the results are stored in the result part of the return, rather than body or message like other APIs sometimes can.

Trying doing a console.log of your entire response ie:

fetch('http://your-modx-site/rest/resources.php') // Change to your endpoint
.then(res => res.json())
.then(json => console.log(json));

You should find that you can then access the results by using json.results. console.log(json.results)

Appreciate the enthusiasm for the documentation and tutorials and personally chalking some things mentioned here to language barriers. I think what would be great now is if you could help contribute what you’ve learnt so far to docs.modx.com as it would inspire and encourage others such as myself, @markh and @digitalpenguin to continue trying to produce content, answer questions and write documentation too.

If you’re not too sure how to help contribute to docs.modx.com @markh created a video hosted on vimeo explaining the process, though I can’t seem to find it off hand but its somewhere in this forum or the fb group :slight_smile:

1 Like

:slight_smile: Actually I am not good in writing as my English and grammar is not very good for that. So I planned to write a complete tutorial with an example of what I learned with REST api for others to understand practically. So I want to make sure everything include in tutorial and nothing should left. So I was seeking some help and I think I asked tooooo much. :smiley:
Please don’t take me wrong. I will post a tutorial and then you guys can add it to documentation for reference. :slight_smile: :slight_smile:

Hey all,
I got success in fetching data via json.
But image tv returning only assets/images/image.jpg instead of full url https://website.com/assets/images/image.jpg

How to get full url of image ?

And I only want to GET data. not PUT, DELETE etc. how to restrict ?
I dont get these lines …

* To disable specific methods, add `beforePost()` , `beforePut()` and `beforeDelete()` methods that return false, or:
* Add the public property `$allowedMethods = array('GET', 'POST', 'PUT', 'DELETE');` with only the appropriate methods. 

@markh :stuck_out_tongue:

What was your issue with getting the JSON in the end?

To disable method literally do what the comments say :wink:

public function beforeDelete() { return false; }

Re documentation don’t worry about language limitation too much as everything goes through an approval process and will be subject to edits and changes from other members. It would be good to just get a base start for restful api’s on docs.modx.com

1 Like

I was calling without 1st Variable for List Item that was .results
and for Single Item it was .object. I totally missed this. :smile:

I added public function beforeDelete() { return false; } and
$allowedMethods = array('GET');

but I still can post from postman. :confused:

here is fully code - https://pastebin.com/Nma2dNQc

ps how to get full image url from image tv as I asked in above??

And as soon as I achieve my goal which is a fully functional rest api (at this point 80% goal achieved )… then I will write documentation for sure. I am super exited for it. :smiley:

Changing your system setting to full urls may post it out as a full value but I’ve not tested it. You can grab any data in your object and modify it though

$data['featuredimage'] = 'https://whatever-value-i-like.com/' . $object->getTVValue('myimagetv'); 

^^ don’t hard code the url like above though use the site option

1 Like

Also you put your allowed methods inside of the read function that’s why it’s not working. It needs to be at the top - try inserting it before public $classKey

1 Like