Hi,

I have coded some rest apis that are included in the folder src/Plugin/rest/resource/ . I have made a custom form where after selecting some options and clicking the submit button, I want to call the respective apis , so that the content get displayed.

I have looked for examples online, but I have not found anything helpful. Could you please let me know how can I go about this?

Thanks,

Comments

dibyajyoti.mallick’s picture

Hi,

Here some of link that help you create Drupal 8 custom rest API:

https://www.valuebound.com/resources/blog/create-rest-resource-for-get-method-drupal-8

https://www.drupal.org/docs/8/api/restful-web-services-api/custom-rest-resources

After create you can test your API through postman is it working or not then after implement in submit button.

Thanks

Saptaparnee’s picture

Thank you for your response. I need to call the rest apis by clicking the submit button of a custom form. This is what I have asked. I have already created the rest apis.

wombatbuddy’s picture

I know two ways to accomplish this.

1. Using JavaScript on the frontend.
You can add to your submit button an Ajax handler.
In this handler you do Ajax request fo the REST service, get a data and render them.
Some examples are here:
https://www.drupal.org/docs/8/core/modules/rest/javascript-and-drupal-8-...
https://www.drupal.org/docs/8/core/modules/rest/javascript-and-drupal-8-...
https://www.droptica.com/blog/drupal-8-restjson-integration-javascript-a...

2. You get data on the backend and render them with Twig template.
See the answer for this question: 'How do I consume REST data with an external service?'.