Requests icon indicating copy to clipboard operation
Requests copied to clipboard

can Requests stringify params and append to the URL?

Open nimmolo opened this issue 10 years ago • 2 comments

From the WordPress HTTP API (which now uses Requests) i'm trying requests to the YouTube and Vimeo APIs and i'm discovering they will not accept the params as part of the HTTP request object. Parameters must be part of the URL. Is this normal?

Works:

$url = 'https://api.vimeo.com/oauth/authorize/client?grant_type=client_credentials&scope=public%20private';
$auth = base64_encode( $developer_key . ':' . $secret_key );
$headers = array(
    'Authorization' => 'Basic ' . $auth,
    'Content-Type' => 'application/json'
);
$args = array(
    'headers'     => $headers
);
$response = wp_remote_post( $url, $args );

Does Not Work:

$url = 'https://api.vimeo.com/oauth/authorize/client';
$auth = base64_encode( $developer_key . ':' . $secret_key );
$data = array(
    'grant_type' => 'client_credentials',
    'scope' => 'public private'
);
$headers = array(
    'Authorization' => 'Basic ' . $auth,
    'Content-Type' => 'application/json'
);
$args = array(
    'headers' => $headers,
    'data' => $data //  ('body' doesn't work either)
);
$response = wp_remote_post( $url, $args );

What is even the difference between these two? Doesn't the service's API decode the string to an object anyway?

Is there a way to get Requests to stringify some parameters and append them to the URL?

nimmolo avatar Nov 06 '16 09:11 nimmolo

Wondering this myself

robwilde avatar Feb 07 '17 14:02 robwilde

Requests will stringify $data if you're making a GET request, it won't do so for POST/PUT/etc requests. There's been previous discussion of this on #228 (see linked Trac tickets), with a mind towards potentially introducing a new parameter for this.

In the WordPress case, you can build them into the URL yourself:

$url = add_query_arg( array_map( 'urlencode', $args ), $url );

rmccue avatar Jun 09 '17 05:06 rmccue