This is because the rest API internally uses json_encode()
to output the data. There are 2 ways that you can resolve this.
1. Prevent the API from sending the data
This might be a bit odd and raise some issues, but you can set the header type and echo the content before returning the data:
header( 'Content-Type: text/event-stream' );
header( 'Cache-Control: no-cache' );
// Echo whatever data you got here
// Prevent the API from continuing
exit();
Since this is not the standard way to use the API, it might cause some issues. I haven’t tried this myself.
2. Use admin-ajax.php
Instead of the rest API, use the admin AJAX. The implementation is the same. Basically if you use json_encode()
alongside with die();
you’ll get the same results as rest API. The codex page on this topic covers almost everything. By using admin AJAX, you can fully control the headers and output type.
Related Posts:
- How to: Make JWT-authenticated requests to the WordPress API
- How would I add custom tables/endpoints to the WP REST API?
- WP REST API create post authentication issue
- WordPress REST API validation
- How do I correctly setup an AJAX nonce for WordPress REST API?
- WordPress Rest API: How do we validate with our custom API key?
- Using the REST API (v2) javascript client on a private namespaced route
- Hiding API routes list
- rest api authentication
- rest_post_query on multiple post types?
- Does accessing WordPress via REST API affect the site stats and analytics?
- Wp Rest Api Custom Endpoint for page subpages
- x-wp-nonce is not allowed by Access-Control-Allow-Headers in preflight response
- How to get around WP REST API per page limit without pagination?
- How to properly add custom entities in Gutenberg
- REST API custom endpoint without authentication for POST method?
- CORS & Remote access to WP via RestAPI
- Create Session with JWT
- WP REST API returns incorrect data?
- WP API ignores filter parameter
- WordPress doesn’t send a notification email when submitting a comment using REST API
- Using WordPress RESTapi to call a php file instead of post or page
- WordPress REST API – Modify JSON before importing
- Is it posible to use wp.data.select(‘core’) outside a block?
- Can you Use the Rest API to query a custom database table
- pagination in WP rest api
- Is there anyway to format my EndPoint URL in WordPress?
- Return WP_Error as WP_REST_Response
- How to use REST API to send user metadata?
- Can the new REST API now make WP into it’s own push notification server?
- Confused about AngularJS and WordPress
- Custom Rest API POST endpoint with conditionally required parameters
- Post API tax_relation field doesn’t work
- What is the meta field in the response of the user REST API?
- How to post an unserialized array via wordpress rest API as meta data
- Is there any way to clear cache when making REST API request?
- Use the backbone.js client to save custom post type meta
- Override WordPress POST REST API
- Creating a custom endpoint for rest, I see the endpoint exists in the wp-json, but the request is returning 404
- WordPress + Vue — Single page app giving me 404s when I use query params
- No ‘Access-Control-Allow-Origin’ when call rest API
- Get subscribers via REST API and send post notification
- Custom API endpoint to create gallery for post
- Register REST route with a multi-value parameter
- How to Get Featured Image from REST API?
- Allow “wp-admin” edit access through headless WP web application
- Change permissions on REST api?
- PHP: authenticate for a REST request?
- Is it Ok to restrict Access-Control-Allow-Origin for /wp-json requests?
- Error invalid parameters with REST API
- How to get featured image in WP rest api
- Authenticate current user to REST API
- rest api endpoint – accept diacritic characters
- How to send the body in wp_remote_post as “raw”?
- REST API and filtering by meta value
- Custom Endpoints not working
- Is there a way I can fetch the WordPress Developer Code References with an API?
- `WP_REST_Controller::get_endpoint_args_for_item_schema` Does Not Set `required` Property from Schema
- Pull in ALL posts from the last two weeks using Rest API
- Extending REST API responses
- 403 error when publishing a post in wordpress. Error => Publishing failed. The response is not a valid JSON response
- Error message: Response is not a valid JSON response
- How to use query parameters, as “_fields”, to filter data inside an array in the REST API?
- WordPress server banning IP
- Cannot use WordPress Application Passwords: “code”: “rest_no_route” “status”:404 for /wp-json/wp/v2/users/me/application-passwords
- How to add Relations of a CCT from JetEngine via WordPress Rest API
- wp_nonce vs jwt
- WP REST API returns empty posts despite entries in wp_posts
- How to add taxonomy to a post using WP REST API?
- The REST API encountered an error in wordpress?
- Do something when publish a post in Gutenberg with hook rest_after_insert_post
- Uploading picture via REST API
- How to modify the HTML output of Gutenberg block? (Youtube)
- Extract XML/JSON element from Zillow API response and populate into Gravity Forms field
- Can I use REST API if the site is protected with .htpasswd
- How can I secure my custom rest api endpoint or add under a already existing rest group
- WordPress single page website redirect to index.html
- How to get the most recently updated orders via the REST API?
- Fetching all users that didn’t post with rest api (current version 2)
- Calling a python script from custom wordpress rest api returns null
- Autotrader API Integration
- WordPress RESTAPI – Restrict unknown
- WordPress Rest API Error 502
- Accessing secure endpoint with X-WP-Nonce?
- Return all custom taxonomy terms for post in REST API v2? Currently limited to 10 terms
- WP Rest API V2 OR Operator in URL
- Call to undefined function upload_is_user_over_quota()
- Rest api request throttling
- Getting current core version from an WordPress installation
- Get custom data from the user REST API endpoint
- Custom rest api endpoint response json problem
- How to block external access to register_rest_route callback?
- Filter output of posts (Rest API)
- JS WordPress API fetch no response headers
- WordPress Application Passwords not authorizing
- Rest API nonce is being cached
- WordPress custom endpoint returns Security violated
- custom REST endpoints and application passwords
- Hide custom posts from certain taxonomy in rest api
- Creating Application Password using REST API results in 401 regardless of JWT token