After some inspecting the WP_REST_Request, it turned out, that the get_body() method was the one I’m looking for. Anyhow, this is what I ended up with:
add_action( 'rest_api_init', function() {
register_rest_route( 'ep', '/as/', [
'methods' => \WP_REST_Server::CREATABLE,
'callback' => 'ep_autosuggest',
] );
} );
function ep_autosuggest( WP_REST_Request $data ) {
// Elasticsearch PHP Client
$client = ClientBuilder::create()->build();
$params = [
'index' => 'ep-test',
'type' => 'post',
'body' => $data->get_body()
];
$response = $client->search( $params );
return $response;
}
For anyone interested, I made a plugin out of it:
https://github.com/grossherr/elasticpress-autosuggest-endpoint
Related Posts:
- Extending wp JavaScript base class to make a post request to a custom REST endpoint
- REST public POST giving 403 forbidden nginx
- How to require files in a custom endpoint
- Custom WP rest api endpoint only working on non https?
- Using Backbone with the WordPress AJAX API
- WP-AJAX vs WP REST API: What to use for requests to the website from outside?
- Including WordPress in RESTful API
- Gutenberg – how to correctly perform ajax request on backend
- AJAX request from Chrome Extension to WordPress Website
- WP REST API route request explain
- Confused on AJAX submit form through page template
- Solve cors problem using rest api or ajax
- Is there a hook to process a backbone restful PUT request inside wordpress?
- How to add WP API and JS featured image attachment
- how to send Ajax request in wordpress backend
- Identical wp_rest nonce returned from rest_api
- How do I get reusable blocks via frontend REST API?
- Is there builtin way to use protected AJAX endpoint?
- rest_no_route custom route
- Test WordPress api with postman
- How to load content from many posts on a page, only if needed
- How do I query posts by a sub value with the API?
- merge wp rest api query to get posts per category does not work
- Ways to load admin-ajax faster without initializing all plugins?
- How to make a fetch() POST request to wordpress rest api?
- post values to custom post type which has advanced custom fields
- Is it good practice to use REST API in wp-admin plugin page? [closed]
- 404 error custom post type rest api
- Create a post with REST API and adding a category
- Should I edit a user meta field with PUT, PATCH, or POST and WP::Editable
- How to trigger lost password email using REST API?
- how reduce fetch/XHR response time
- Why are the most recent posts not appearing in a fetch request, unless I’m logged in?
- How to submit a button automatically after every scheduled hours?
- WordPress REST API FormData: Form Not Submitted When No Files Attached
- register_rest_route to send via ajax as guest results to 403
- How to cache json with wp-super cache
- Get Previous & Next posts by Post ID
- Ajax requests without JQuery
- Custom PHP endpoint for a plugin’s AJAX call
- How to stop WP API endpoint from caching?
- WordPress Ajax Login without page reload
- AJAX only works when I’m logged as Admin
- When using javascript to dynamically add a textarea, how can I render it as a tinyMCE editor (in wordpress)?
- Custom ordering fails after AJAX post filter
- How to securely add an Ajax button to a WP Admin page?
- WordPress Rest API – Get all posts based on post_meta on custom endpoint
- Prevent page reload after ajax form submission
- WordPress Ajax Getting Response “Null”
- admin-ajax return error 400 for Chrome/Edge but not Firefox
- Is it safe to manually sign a user in using AJAX?
- WordPress Admin AJAX Serialize
- Why a strange discrepency between get_current_user_id() when using AJAX versus output of document.cookie?
- Loading post template that contains a nested loop with ajax
- Long Polling: Stuck in while loop
- Show Post Content with AJAX
- Code run fast on a cronjob but slow with a do_action or ajax call
- update_post_meta using ajax
- Ajax with plugins returns 0
- Ajax comment form submit on frontpage alerts success but no insertion
- Wp mail does not work when Ajaxed to
- All AJAX requests return a 400 error
- Unable to successfully verify nonce
- Create custom POST Method URL
- Getting a variable using $post ajax back from php to js response in WP
- AJAX Call – Failed to load resource: the server responded with a status of 500 (Internal Server Error)
- WordPress is Not Setting PHP $_POST on Custom Ajax
- WP Rest Api / Ajax POST not working when not logged in
- bulk update meta value with ajax
- Is it possible to determine whether a page is a page template by page_id in ajax call?
- How to stop being directed to admin.php after sending request to admin-ajax.php
- 200 return code on ‘POST /wp-admin/admin-ajax.php’ while NOT logged in
- Next Ajax call doesn’t work
- Ajax page load without reload
- need a confirmation text to appear on email submission
- How to make pages that arent indexed for ajax applications
- Why does reCAPTCHA v3 return 0 ? In custom AJAX registration form
- admin-ajax.php 400 bad request
- How to do admin ajax request in a plugin for rest api
- WordPress Ajax Page Load to skip embedded iframe
- WordPress POST AJAX call, var_dump($_POST) NULL and AJAX response empty when inside ob_start
- Set cors header for ajax requests
- Simple AJAX code that refreshes every x seconds?
- Search function – problem with whole words
- Ajax is not defined
- Get posts by category via ajax
- How can i get the same ajax result using WP REST API instead of admin-ajax?
- WordPress 4.9.6 – IncludeMe & getAjax GET using wrong URL
- Create secondary Archive page format?
- Custom Rest API namespace and endpoints are responding with 404 & 503 errors
- Weird admin-ajax.php problem
- Ajax contact form returnig 0
- Can’t update WP Editor after Ajax
- retrieving page content per AJAX [duplicate]
- Posting to loop.php file
- Can’t access data from database using AJAX
- Running js in html code with same content
- How to get current_user_id from wordpress in node js?
- how can i create get request unis ajax without expecting return value
- AJAX call not initializing for non-admins in WordPress