Skip to main content

Request Body

In Phlame's API development toolkit, the body method within the API class provides a straightforward approach to handle HTTP request bodies in incoming requests. This method offers developers flexibility and simplicity, enabling them to process request payloads efficiently and execute custom logic based on the contents. Let's explore the syntax and usage of the body method:

Retrieving Request Body

The body method allows developers to access the content of the HTTP request body. Here's how you can use it:

Api::body():

This method retrieves the entire request body and returns it as a string, with a status, true, if the body exists.

Api::body();
note

Other functions of the Body method functions similar to the headers method, it takes the same parameters as the headers method, check the headers method out for more information

Handling Results with Callbacks

Similar to the headers method, the body method also supports then: and orelse: callbacks, allowing developers to execute custom logic based on the contents of the request body:

Then Callback:

This callback runs when the body retrieval is successful, and the status is true, providing developers with the request body content. It allows for processing and manipulation of the request data.

Orelse Callback:

In case the body retrieval fails, and the status is false, the orelse: callback is executed. This provides developers with a fallback mechanism to handle such scenarios gracefully.

tip

You can utilize the $rout variable within the callback functions to access the data returned by the body method, which includes:

  • Status
  • Data

Example of the Body Method

<filename>.src.php
<?php
Api::body(
key: "key",
value: "value",
then: function($rout) {
// Custom logic to process the request body
$body = $rout['data']; // Accessing the request body content
},
orelse: function() {
// Fallback logic for handling empty or malformed request bodies
}
);
?>

This example illustrates how to use the body method to retrieve and process the content of the HTTP request body. Depending on the success or failure of the body retrieval, the appropriate callback is executed, allowing developers to handle request body processing effectively.