Opened 8 years ago
Last modified 18 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: | 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)
Change History (19)
@
8 years ago
Added TRACE functions to previous patch. Improved documentation alignment. Few spacing improvements.
#3
in reply to:
↑ description
@
8 years ago
Replying to bhubbard:
While we have
wp_remote_post()
andwp_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.
8 years ago
#5
@
8 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
@
8 years ago
Should we consider supporting any of the other/newer HTTP Methods?
PATCH COPY LINK UNLINK PURGE LOCK UNLOCK PROPFIND VIEW
#9
@
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
@
6 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
@
6 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).
#13
@
4 years ago
I'd love to see a wp_remote_put()
GET
andPOST
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
;)
#15
follow-up:
↓ 16
@
18 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
@
18 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 );
Patch for DELETE and PUT HTTP calls