Request API Reference
The complete API reference for the Request class in the Fetch HTTP client package.
Class Declaration
namespace Fetch\Http;
class Request extends BaseRequest implements RequestInterface
{
use RequestImmutabilityTrait;
// ...
}Constructor
/**
* Create a new Request instance.
*/
public function __construct(
string|Method $method,
string|UriInterface $uri,
array $headers = [],
$body = null,
string $version = '1.1',
?string $requestTarget = null
)Static Factory Methods
HTTP Method Factories
get()
Create a new GET request.
public static function get(string|UriInterface $uri, array $headers = []): staticpost()
Create a new POST request.
public static function post(
string|UriInterface $uri,
$body = null,
array $headers = [],
ContentType|string|null $contentType = null
): staticput()
Create a new PUT request.
public static function put(
string|UriInterface $uri,
$body = null,
array $headers = [],
ContentType|string|null $contentType = null
): staticpatch()
Create a new PATCH request.
public static function patch(
string|UriInterface $uri,
$body = null,
array $headers = [],
ContentType|string|null $contentType = null
): staticdelete()
Create a new DELETE request.
public static function delete(
string|UriInterface $uri,
$body = null,
array $headers = [],
ContentType|string|null $contentType = null
): statichead()
Create a new HEAD request.
public static function head(string|UriInterface $uri, array $headers = []): staticoptions()
Create a new OPTIONS request.
public static function options(string|UriInterface $uri, array $headers = []): staticContent Type Factories
json()
Create a new Request instance with a JSON body.
public static function json(
string|Method $method,
string|UriInterface $uri,
array $data,
array $headers = []
): staticform()
Create a new Request instance with form parameters.
public static function form(
string|Method $method,
string|UriInterface $uri,
array $formParams,
array $headers = []
): staticmultipart()
Create a new Request instance with multipart form data.
public static function multipart(
string|Method $method,
string|UriInterface $uri,
array $multipart,
array $headers = []
): staticRequest Target Methods
getRequestTarget()
Get the request target (path for origin-form, absolute URI for absolute-form, authority for authority-form, or asterisk for asterisk-form).
public function getRequestTarget(): stringwithRequestTarget()
Return an instance with the specific request target.
public function withRequestTarget($requestTarget): staticRequest Method Information
getMethodEnum()
Get the method as an enum.
public function getMethodEnum(): ?MethodsupportsRequestBody()
Check if the request method supports a request body.
public function supportsRequestBody(): boolContent Type Methods
getContentTypeEnum()
Get the content type from the headers as an enum.
public function getContentTypeEnum(): ?ContentTypehasJsonContent()
Check if the request has JSON content.
public function hasJsonContent(): boolhasFormContent()
Check if the request has form content.
public function hasFormContent(): boolhasMultipartContent()
Check if the request has multipart content.
public function hasMultipartContent(): boolhasTextContent()
Check if the request has text content.
public function hasTextContent(): boolBody Methods
getBodyAsString()
Get the request body as a string.
public function getBodyAsString(): stringgetBodyAsJson()
Get the request body as JSON.
public function getBodyAsJson(bool $assoc = true, int $depth = 512, int $options = 0): mixedgetBodyAsFormParams()
Get the request body as form parameters.
public function getBodyAsFormParams(): arrayRequest Modification Methods
withBody()
Return an instance with the specified body.
public function withBody($body): staticwithContentType()
Set the content type of the request.
public function withContentType(ContentType|string $contentType): staticwithQueryParam()
Set a query parameter on the request URI.
public function withQueryParam(string $name, string|int|float|bool|null $value): staticwithQueryParams()
Set multiple query parameters on the request URI.
public function withQueryParams(array $params): staticwithBearerToken()
Set an authorization header with a bearer token.
public function withBearerToken(string $token): staticwithBasicAuth()
Set a basic authentication header.
public function withBasicAuth(string $username, string $password): staticwithJsonBody()
Set a JSON body on the request.
public function withJsonBody(array $data, int $options = 0): staticwithFormBody()
Set a form body on the request.
public function withFormBody(array $data): staticPSR-7 Methods (from RequestImmutabilityTrait)
These methods override the PSR-7 request methods to ensure immutability and proper type preservation.
withAddedHeader()
Return an instance with the specified header appended with the given value.
public function withAddedHeader($name, $value): staticwithoutHeader()
Return an instance without the specified header.
public function withoutHeader($name): staticwithHeader()
Return an instance with the provided value replacing the specified header.
public function withHeader($name, $value): staticwithProtocolVersion()
Return an instance with the specified protocol version.
public function withProtocolVersion($version): staticwithUri()
Return an instance with the specified URI.
public function withUri(UriInterface $uri, $preserveHost = false): staticwithMethod()
Return an instance with the provided HTTP method.
public function withMethod($method): static