Make WordPress Core

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#35583 closed defect (bug) (duplicate)

$HTTP_RAW_POST_DATA error on POST request to REST API

Reported by: jhoffm34's profile jhoffm34 Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.4
Component: REST API Keywords:
Focuses: Cc:

Description

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

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

Change History (2)

#1 @ocean90
9 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed
  • Version changed from 4.4.1 to 4.4

Hello @jhoffm34, welcome to Trac!

We already have a similar report about this in #31806.

Quote by dd32:

Since we don't require the presence of HTTP_RAW_POST_DATA (We already include back-compat, and now, forward-compat for it) and the warning can't be controlled by WordPress, there doesn't appear to be anything we can do about this.

#2 @jhoffm34
9 years ago

Thanks! Got it, didn't catch that. Seems like the questions answered.

Note: See TracTickets for help on using tickets.