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
- How do i set up ajax nonce
- WooCommerce Order Sync via Custom REST API Endpoint Failing
- register_rest_route to send via ajax as guest results to 403
- PHP “php://input” vs $_POST
- Google Maps API throws “Uncaught ReferenceError: google is not defined” only when using AJAX
- Access-Control-Allow-Origin error sending a jQuery Post to Google API’s
- Access-Control-Allow-Origin error sending a jQuery Post to Google API’s
- How to solve the error “SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied.” in IE
- Show spinner GIF during an $http request in AngularJS?
- Refresh HTML Page in Browser Automatically on Timer – Every 15 Min
- JavaScript implementation of Gzip
- jQuery: Performing synchronous AJAX requests
- ASP.NET MVC controller actions that return JSON or partial html
- jQuery’s .on() method combined with the submit event
- Ajax takes 10x as long as it should/could
- How to check if I am in admin-ajax.php?
- Best way to end WordPress ajax request and why?
- How to load wp_editor() through AJAX/jQuery
- WordPress Rest API custom endpoint optional param
- Using the Rewrite API to Construct a RESTful URL
- How to cache json with wp-super cache
- Load minimum WordPress environment
- Why use wp_send_json() over echo json_encode()?
- Why use admin-ajax.php and how does it work?
- How to get a unique nonce for each Ajax request?
- Open a Thickbox with content trough AJAX
- Initialize TinyMCE editor / visual editor after AJAX insert
- Why not register shortcodes if is_admin dashboard?
- WordPress AJAX with Axios
- Why is die() used at the end of function that handles an Ajax request?
- Why might a plugin’s ‘do_shortcode’ not work in an AJAX request?
- Making my AJAX powered WordPress Crawlable
- Is there a JavaScript API? How to access public and private data in JS?
- Get Previous & Next posts by Post ID
- failed to load wp-admin/admin-ajax.php
- Using Ajax with a Class file
- Ajax in a settings page (update_option is undefined)
- How to pass data parameter to ajax action function handler
- WordPress Ajax Data Security
- Displaying PHP Errors from admin-ajax.php
- wp_set_auth_cookie() doesn’t work in Ajax call
- gettext does not translate when called in ajax
- Execute one AJAX request after another AJAX request finished
- Ajax and autocomplete
- How to add to cart via AJAX Woocommerce [closed]
- Nonces and Cache
- SSL breaks customizer: page isn’t returned from ajax
- Why is a 500 error generated by admin-ajax.php not going into the Apache error log?
- Are ‘wp_ajax’ and ‘wp_ajax_nopriv’ exclusive to authenticated and non-authenticated users?
- How to HTML5 FormData Ajax
- admin-ajax.php vs Custom Page Template for Ajax Requests
- How to override WP_DEBUG for Ajax responses?
- Why is my custom API endpoint not working?
- Stop admin-ajax?
- redirect out of wp-admin, without losing admin-ajax.php
- Call to undefined function add_action()
- Is it safe to assume that a nonce may be validated more than once?
- WooCommerce: Can’t use wc_get_products for custom REST API endpoints
- When adding a custom REST endpoint, where do you put the endpoint function, and where do you put the function registration call?
- Multiple ajax nonce requests
- Get posts with ajax
- admin-ajax.php doesn’t work when using POST data and Axios
- How to call a PHP function with Ajax when the user clicks a button
- Ajax for non-logged-in users
- Contact Form 7 Custom Post Action