Make WordPress Core

Opened 7 years ago

Last modified 11 months ago

#40142 new enhancement

WP Remote Remaining Methods

Reported by: bhubbard's profile bhubbard Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 2.7
Component: HTTP API Keywords: needs-patch 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 7 years ago.
Patch for DELETE and PUT HTTP calls
40142_2.patch (5.1 KB) - added by sgarza 7 years ago.
Revision to original patch(comments and formatting)
40142.2.patch (7.2 KB) - added by desrosj 7 years ago.
Added TRACE functions to previous patch. Improved documentation alignment. Few spacing improvements.

Download all attachments as: .zip

Change History (19)

@sgarza
7 years ago

Patch for DELETE and PUT HTTP calls

@sgarza
7 years ago

Revision to original patch(comments and formatting)

#1 @johnbillion
7 years ago

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

@desrosj
7 years ago

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

#2 @desrosj
7 years ago

  • Keywords needs-unit-tests added

#3 in reply to: ↑ description @bhubbard
7 years 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.


7 years ago

#5 @johnbillion
7 years 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
7 years ago

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

PATCH
COPY
LINK
UNLINK
PURGE
LOCK
UNLOCK
PROPFIND
VIEW

#7 @bhubbard
7 years ago

  • Focuses rest-api added

#8 @jnylen0
7 years ago

  • Focuses rest-api removed

This isn't specifically related to the REST API.

#9 @dd32
7 years 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 ) );

#10 @bhubbard
5 years ago

I was wondering if someone could revisit this ticket? @dd32 How about one of these for names:

wp_safe_remote_build_request()
wp_safe_remote_make_request()
wp_safe_remote_start_request()
wp_safe_remote_do_request()

#11 @dd32
5 years ago

  • Keywords needs-patch added; has-patch 2nd-opinion removed

FWIW my suggested method moving forward here would be to overload wp_safe_remote_request(). I won't be taking charge of implementing this though.

Changing the signature from wp_safe_remote_request( $url, $args ) to wp_safe_remote_request( $method, $url, $args, $body ) with appropriate back-compat in place (if $method is url and $url is array, assume old calling method, transparently upgrade request to new syntax).

#12 @desrosj
3 years ago

#52424 was marked as a duplicate.

#13 @danielbachhuber
3 years ago

I'd love to see a wp_remote_put()

GET and POST are used enough to warrant a top-level function, but the long tail can probably be catered to better.

I'd think PUT and DELETE are used as often as HEAD ;)

#14 @johnbillion
3 years ago

I see no reason not to introduce wrapper functions for all the methods.

#15 follow-up: @pingram3541
11 months ago

I know this is an old one and if there is a better approach than making our own function like:

function wp_safe_remote_delete( $url, $args = array() ) {
	$args['reject_unsafe_urls'] = true;
	$http                       = _wp_http_get_object();
	return $http->delete( $url, $args );
}

I'd love to know. Otherwise, I also like the idea of passing the method via $args to a universal function like:

wp_safe_remote_http_request( $url, $args );

#16 in reply to: ↑ 15 @pingram3541
11 months ago

I was close, this already exists with wp_remote_request & wp_safe_remote_request.

ex.

$response = wp_safe_remote_request(
  $post_url,
  [
    'method'  => 'PUT', //DELETE, PATCH, etc.
    'headers' => [
      'Content-Type'  => 'application/json; charset=utf-8',
      'Authorization' => $token
    ],
    'body'    => [
      'some_setting'       => $some_value
    ]
  ]
);

Replying to pingram3541:

I know this is an old one and if there is a better approach than making our own function like:

function wp_safe_remote_delete( $url, $args = array() ) {
	$args['reject_unsafe_urls'] = true;
	$http                       = _wp_http_get_object();
	return $http->delete( $url, $args );
}

I'd love to know. Otherwise, I also like the idea of passing the method via $args to a universal function like:

wp_safe_remote_http_request( $url, $args );
Note: See TracTickets for help on using tickets.