[wp-trac] [WordPress Trac] #35583: $HTTP_RAW_POST_DATA error on POST request to REST API
WordPress Trac
noreply at wordpress.org
Fri Jan 22 21:44:43 UTC 2016
#35583: $HTTP_RAW_POST_DATA error on POST request to REST API
--------------------------+-----------------------------
Reporter: jhoffm34 | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: REST API | Version: 4.4.1
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
I'm currently using just the infrastructure of this plugin on WP version
4.4.1. I'm getting a weird error when setting up a POST request:
{{{
Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be
removed in a future version. To avoid this warning set
'always_populate_raw_post_data' to '-1' in php.ini and use the php://input
stream instead. in Unknown on line 0.
}}}
GET requests are fine, there is only the error returned on POST requests.
The function that I'm using (simplified somewhat) is:
{{{#!php
<?php
function init() {
add_action( 'rest_api_init', 'add_endpoint_to_rest_api' );
}
function add_endpoint_to_rest_api() {
$namespace = 'endpoint/v1';
$enpdpoint = 'custom_type';
register_rest_route( $namespace, '/' . $endpoint . '/', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'endpoint_get_all',
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => 'endpoint_create_single'
)
));
}
function endpoint_create_single( $request ) {
if ( ! empty( $request['id'] ) ) {
return new WP_Error( 'rest_post_exists', __( 'Cannot create
existing post.' ), array( 'status' => 400 ) );
}
$prepared_post = new stdClass;
// Post title.
if ( isset( $request['title'] ) ) {
if ( is_string( $request['title'] ) ) {
$prepared_post->post_title = wp_filter_post_kses(
$request['title'] );
}
}
$prepared_post->post_type = 'custom_type';
$prepared_post->post_status = 'publish';
$post_id = wp_insert_post( $prepared_post, true );
$response = array(
'update' => 'true'
);
$response = rest_ensure_response( $response );
$response->set_status( 201 );
return $response;
}
}}}
Even if I completely edit the callback function to just return a plain
response, I still get the same error. I can fix it by editing my php.ini
file and changing "always_populate_raw_post_data" to -1 like it suggests,
but I'm wondering why this is an error on a default setup, with php 5.6
installed, and figured I should report it.
I believe this may be one of the sources :
https://core.trac.wordpress.org/browser/trunk/src/wp-includes/rest-api
/class-wp-rest-server.php#L1176
--
Ticket URL: <https://core.trac.wordpress.org/ticket/35583>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list