Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ on the internal execution path, so it's recommended to set this explicitly if
you need to.


Make a QUERY Request
--------------------
The [`QUERY`][rfc10008] method (RFC 10008) is like `GET`, but sends its data in
the request body instead of the URL query string. This lets you send large or
complex queries while keeping the safe and idempotent semantics of `GET`:

```php
$data = array('key1' => 'value1', 'key2' => 'value2');
$response = \WpOrg\Requests\Requests::query('https://httpbin.org/anything', array(), $data);
```

As with `POST`, you can pass a string instead of an array to send raw data, and
you'll probably want to set the `Content-Type` header to match.

[rfc10008]: https://tools.ietf.org/html/rfc10008


Status Codes
------------
The Response object also gives you access to the status code:
Expand Down
22 changes: 21 additions & 1 deletion src/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ class Requests {
*/
const PATCH = 'PATCH';

/**
* QUERY method
*
* @link https://tools.ietf.org/html/rfc10008
* @var string
*/
const QUERY = 'QUERY';

/**
* Default size of buffer size to read streams
*
Expand Down Expand Up @@ -365,6 +373,18 @@ public static function options($url, $headers = [], $data = [], $options = []) {
public static function patch($url, $headers, $data = [], $options = []) {
return self::request($url, $headers, $data, self::PATCH, $options);
}

/**
* Send a QUERY request
*
* Note: Unlike {@see \WpOrg\Requests\Requests::get()}, the `$data` is sent in the
* request body (like POST), not appended to the URL as a query string.
*
* @link https://tools.ietf.org/html/rfc10008
*/
public static function query($url, $headers = [], $data = [], $options = []) {
return self::request($url, $headers, $data, self::QUERY, $options);
}
/**#@-*/

/**
Expand Down Expand Up @@ -416,7 +436,7 @@ public static function patch($url, $headers, $data = [], $options = []) {
* (bool, default: true)
* - `data_format`: How should we send the `$data` parameter?
* (string, one of 'query' or 'body', default: 'query' for
* HEAD/GET/DELETE, 'body' for POST/PUT/OPTIONS/PATCH)
* HEAD/GET/DELETE, 'body' for POST/PUT/OPTIONS/PATCH/QUERY)
*
* @param string|\Stringable $url URL to request
* @param array $headers Extra headers to send with the request
Expand Down
17 changes: 17 additions & 0 deletions tests/Transport/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,23 @@
$this->assertSame(['test' => 'true', 'test2' => 'test'], $result['form']);
}

public function testQUERY() {
$request = Requests::query($this->httpbin('/query'), [], [], $this->getOptions());
$this->assertSame(200, $request->status_code);

Check failure on line 575 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.2

Failed asserting that 501 is identical to 200.

Check failure on line 575 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.2

Failed asserting that 501 is identical to 200.

Check failure on line 575 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.6

Failed asserting that 404 is identical to 200.

Check failure on line 575 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.6

Failed asserting that 404 is identical to 200.

Check failure on line 575 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.1

Failed asserting that 501 is identical to 200.

Check failure on line 575 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.1

Failed asserting that 501 is identical to 200.

Check failure on line 575 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.1

Failed asserting that 501 is identical to 200.

Check failure on line 575 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.1

Failed asserting that 501 is identical to 200.

Check failure on line 575 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.3

Failed asserting that 501 is identical to 200.

Check failure on line 575 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.3

Failed asserting that 501 is identical to 200.

Check failure on line 575 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.2

Failed asserting that 501 is identical to 200.

Check failure on line 575 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.4

Failed asserting that 501 is identical to 200.

Check failure on line 575 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.2

Failed asserting that 501 is identical to 200.

Check failure on line 575 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.4

Failed asserting that 501 is identical to 200.

Check failure on line 575 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.4

Failed asserting that 501 is identical to 200.

Check failure on line 575 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.4

Failed asserting that 501 is identical to 200.

Check failure on line 575 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.3

Failed asserting that 501 is identical to 200.

Check failure on line 575 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.3

Failed asserting that 501 is identical to 200.

Check failure on line 575 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.5

Failed asserting that 501 is identical to 200.

Check failure on line 575 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.5

Failed asserting that 501 is identical to 200.
}

public function testQUERYWithData() {
$data = [
'test' => 'true',
'test2' => 'test',
];
$request = Requests::query($this->httpbin('/query'), [], $data, $this->getOptions());
$this->assertSame(200, $request->status_code);

Check failure on line 584 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.2

Failed asserting that 501 is identical to 200.

Check failure on line 584 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.2

Failed asserting that 501 is identical to 200.

Check failure on line 584 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.6

Failed asserting that 404 is identical to 200.

Check failure on line 584 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.6

Failed asserting that 404 is identical to 200.

Check failure on line 584 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.1

Failed asserting that 501 is identical to 200.

Check failure on line 584 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.1

Failed asserting that 501 is identical to 200.

Check failure on line 584 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.1

Failed asserting that 501 is identical to 200.

Check failure on line 584 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.1

Failed asserting that 501 is identical to 200.

Check failure on line 584 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.3

Failed asserting that 501 is identical to 200.

Check failure on line 584 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.3

Failed asserting that 501 is identical to 200.

Check failure on line 584 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.2

Failed asserting that 501 is identical to 200.

Check failure on line 584 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.4

Failed asserting that 501 is identical to 200.

Check failure on line 584 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.2

Failed asserting that 501 is identical to 200.

Check failure on line 584 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.4

Failed asserting that 501 is identical to 200.

Check failure on line 584 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.4

Failed asserting that 501 is identical to 200.

Check failure on line 584 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.4

Failed asserting that 501 is identical to 200.

Check failure on line 584 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.3

Failed asserting that 501 is identical to 200.

Check failure on line 584 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.3

Failed asserting that 501 is identical to 200.

Check failure on line 584 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.5

Failed asserting that 501 is identical to 200.

Check failure on line 584 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.5

Failed asserting that 501 is identical to 200.

$result = json_decode($request->body, true);
$this->assertSame(['test' => 'true', 'test2' => 'test'], $result['form']);
}

public function testRedirects() {
$request = Requests::get($this->httpbin('/redirect/6'), [], $this->getOptions());
$this->assertSame(200, $request->status_code);
Expand Down Expand Up @@ -919,7 +936,7 @@
}

$request = Requests::head('https://humanmade.com/', [], $this->getOptions($options));
$this->assertSame(200, $request->status_code);

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.2

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.2

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.2

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.2

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.2

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.2

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.6

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.6

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.6

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.6

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.6

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.6

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.1

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.1

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.1

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.1

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.1

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.1

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.1

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.1

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.1

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.1

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.1

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.1

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.3

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.3

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.3

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.3

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.3

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.3

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.2

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.2

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.2

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.4

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.4

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.4

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.2

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.2

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.4

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.2

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.4

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 7.4

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.4

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.4

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.4

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.4

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.4

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.4

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.3

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.3

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.3

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.3

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.3

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.3

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.5

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.5

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.5

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.5

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.5

Failed asserting that 403 is identical to 200.

Check failure on line 939 in tests/Transport/BaseTestCase.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.5

Failed asserting that 403 is identical to 200.
}

/**
Expand Down
Loading