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?
- Google Maps API throws “Uncaught ReferenceError: google is not defined” only when using AJAX
- Initialize TinyMCE editor / visual editor after AJAX insert
- Displaying PHP Errors from admin-ajax.php
- wp_set_auth_cookie() doesn’t work in Ajax call
- Is it safe to assume that a nonce may be validated more than once?
- When adding a custom REST endpoint, where do you put the endpoint function, and where do you put the function registration call?
- How to properly use wp.ajax.post?
- AJAX vs Fragment Caching for W3 Total Cache [closed]
- Delete row of custom table in WordPress using AJAX
- jQuery Autocomplete in WordPress
- register_rest_field for custom taxonomy fields that are assosiated with custom post type
- Change theme based on window size
- Class WP_Rewrite not found
- How to allow to user non logged in WP system upload in media library?
- Continue execution after WP REST API response
- WordPress function to get term or post object by id
- Conditional success callback based on Ajax Response not working
- I’m getting a 401 while calling the wp-json endpoint via ajax
- Keep getting 400 Bad Request error when trying out AJAX on custom theme
- Ajax simple experiment
- How to load new text widgets and polls into the sidebar without reloading the page?
- Are nonces in WP REST API optional by default?
- Admin ajax add tag callback
- Stop auto load wp-admin admin-ajax.php
- Save & Reset button in theme-option with Ajax (without refresh)
- Ajax Multi Response Problem
- AJAX call inside plugin class not triggering
- Custom AJAX Endpoint not returning any result
- Contact form 7 Hide response messages after 5 seconds
- how should i get json encoded data from wordpress ajax action page
- Cache plugins and ajax nonce verification
- Ajax function on #publish only saves as draft – how to make it publish?
- AJAX call broken after actualization
- custom autocomplete search
- JSON – Create rest api endpoint for Advanced Custom Fields
- ajax problem – function on server is not called
- wp_mail doesn’t work when logged in?
- Need help with Access-Control-Allow-Origin
- Send email via ajax
- Load page HTML content through AJAX
- problem using ajax url
- How to display contact form 7 form in vanilla js without jquery in frontend
- Error sending array data from php to javascript
- Filter out posts based on a certain post_meta on the WordPress REST API
- Sharing variables in both actions wp_footer and wp_ajax
- Display full WordPress post under search form using AJAX
- Ajax callback not work
- jQuery.post returns 0
- Opening Modal popup on Ajax form submission
- Ajax sometimes work and sometimes just don’t work
- admin-ajax error 400
- wordpress admin ajax trash_comment
- Failed to load resource: the server responded with a status of 404 (Not Found) admin-ajax.php
- Javascript output now showing in custom widget
- how to correctly use json_encode with ajax
- Retrieve Header Background Image with AJAX
- How to block search engines indexing certain AJAX actions
- Ajax Comment: Page reloads whenever the comment submission form is reloaded
- wp_ajax_action, wp_ajax_nopriv_action not working
- Manually cache a special, non-WP-but-using-WP page (e.g. Ajax results) with W3TC
- Is it a good idea to init ajax functions only while ‘DOING_AJAX’
- Static var overwritten with WP AJAX action, works fine without AJAX
- AJAX requests within templates
- using ajax to query sql
- Bad Request 400… jQuery ajax post of json data to wordpress admin-ajax.php
- At what stage does wp_ajax hooks gets applied during WordPress request?