WordPress.org

Make WordPress Core

Opened 15 months ago

Last modified 12 months ago

#40142 new enhancement

WP Remote Remaining Methods

Reported by: bhubbard Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 2.7
Component: HTTP API Keywords: has-patch 2nd-opinion needs-unit-tests
Focuses: Cc:

Description

While we have wp_remote_post() and wp_remote_get I would like to see helper functions for the remaining HTTP Methods. These can be very useful working with third-party APIs, and as the Rest API becomes more important.

Requested Functions:

wp_remote_delete() wp_remote_put() wp_remote_trace() wp_remote_get_head() wp_remote_get_options() wp_remote_connect()

Further Reading: https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

Attachments (3)

40142.patch (4.5 KB) - added by sgarza 15 months ago.
Patch for DELETE and PUT HTTP calls
40142_2.patch (5.1 KB) - added by sgarza 15 months ago.
Revision to original patch(comments and formatting)
40142.2.patch (7.2 KB) - added by desrosj 15 months ago.
Added TRACE functions to previous patch. Improved documentation alignment. Few spacing improvements.

Download all attachments as: .zip

Change History (12)

@sgarza
15 months ago

Patch for DELETE and PUT HTTP calls

@sgarza
15 months ago

Revision to original patch(comments and formatting)

#1 @johnbillion
15 months ago

  • Keywords has-patch 2nd-opinion added
  • Version changed from trunk to 2.7

@desrosj
15 months ago

Added TRACE functions to previous patch. Improved documentation alignment. Few spacing improvements.

#2 @desrosj
15 months ago

  • Keywords needs-unit-tests added

#3 in reply to: ↑ description @bhubbard
15 months ago

Replying to bhubbard:

While we have wp_remote_post() and wp_remote_get I would like to see helper functions for the remaining HTTP Methods. These can be very useful working with third-party APIs, and as the Rest API becomes more important.

Requested Functions:

wp_remote_delete() wp_remote_put() wp_remote_trace() wp_remote_head() wp_remote_options() wp_remote_connect()

Further Reading: https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

This ticket was mentioned in Slack in #core-http by sgarza. View the logs.


14 months ago

#5 @johnbillion
14 months ago

It looks like there's no test coverage for the existing wp_remote_get() and wp_remote_post() functions with regard to the HTTP method they use.

A test could look something like this, with test_remote_methods() using a dataProvider for each of the supported methods.

function test_remote_methods( $method, $expected_http_method ) {
	add_filter( 'pre_http_request', array( $this, 'http_catcher' ), 10, 2 );

	call_user_func( "wp_remote_{$method}", 'http://example.com' );

	remove_filter( 'pre_http_request', array( $this, 'http_catcher' ), 10, 2 );

	$this->assertSame( $this->http_args['method'], $expected_http_method );
}

function http_catcher( $preempt, $args ) {
	// log the args
	$this->http_args = $args;
	// short-circuit the request
	return true;
}

#6 @bhubbard
14 months ago

Should we consider supporting any of the other/newer HTTP Methods?

PATCH
COPY
LINK
UNLINK
PURGE
LOCK
UNLOCK
PROPFIND
VIEW

#7 @bhubbard
12 months ago

  • Focuses rest-api added

#8 @jnylen0
12 months ago

  • Focuses rest-api removed

This isn't specifically related to the REST API.

#9 @dd32
12 months ago

Personally I don't think we should add wrappers for each potential method.

GET and POST are used enough to warrant a top-level function, but the long tail can probably be catered to better. We made a huge mistake when we added the wp_remote_post() method IMHO, although it sets the method parameter it didn't make it easier to add the body to the request, resulting in developers having to specify $args still.

Perhaps a new generic function could be offered which spells out the request a little simpler, I don't think they're needed on the WP_HTTP class directly though as that's not an API developers should be interacting with.

function wp_safe_remote_SOMETHING(
     $method = 'GET',
     $url = '',
     $args,
     $body = null
) {
	$args['method'] = $method;
	$args['body'] = $body;
	$args['reject_unsafe_urls'] = true;

	$http = _wp_http_get_object();
	return $http->request( $url, $args );
}

usage: $request = wp_safe_remote_SOMETHING( 'PURGE', 'https://dd32.id.au/', array( 'timeout' => 1 ), array( 'force' => 1 ) );

Note: See TracTickets for help on using tickets.