WordPress\AiClient\Providers\Http\DTO

Request::getBodypublicWP 0.1.0

Gets the request body.

For GET requests, returns null. For POST/PUT/PATCH requests:

  • If body is set, returns it as-is
  • If data is set and Content-Type is JSON, returns JSON-encoded data
  • If data is set and Content-Type is form, returns URL-encoded data

Method of the class: Request{}

No Hooks.

Returns

String|null. The body.

Usage

$Request = new Request();
$Request->getBody(): ?string;

Changelog

Since 0.1.0 Introduced.

Request::getBody() code WP 7.0.4

public function getBody(): ?string
{
    // GET requests don't have a body
    if (!$this->method->hasBody()) {
        return null;
    }
    // If body is set, return it as-is
    if ($this->body !== null) {
        return $this->body;
    }
    // If data is set, encode based on content type
    if ($this->data !== null) {
        $contentType = $this->getContentType();
        // JSON encoding
        if ($contentType !== null && stripos($contentType, 'application/json') !== \false) {
            return json_encode($this->data, \JSON_THROW_ON_ERROR);
        }
        // Default to URL encoding for forms
        return http_build_query($this->data);
    }
    return null;
}