Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the coder-elementor domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u262393194/domains/codestap.com/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rank-math domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u262393194/domains/codestap.com/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rocket domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u262393194/domains/codestap.com/public_html/wp-includes/functions.php on line 6114
How do you handle request and response bodies in Slim Framework? - Code Stap
How do you handle request and response bodies in Slim Framework?

How do you handle request and response bodies in Slim Framework?

In Slim Framework, handling request and response bodies is straightforward. Here’s how you manage both:

Handling Request Body

  • Get Request Body as String: To retrieve the entire request body as a string, use the getBody() method:

Example

<?php
$app->post('/process', function ($request, $response) {
    $body = (string) $request->getBody();
    // Process the body content
    return $response->write('Request received: ' . $body);
});
?>
  • Get Request Body as JSON: If you’re expecting a JSON request, you can decode it into an associative array:

Example

<?php
$app->post('/json', function ($request, $response) {
    $data = $request->getParsedBody();
    // Access JSON data, e.g., $data['key']
    return $response->withJson($data);
});
?>
  • Get Form Data: To get form-encoded data (e.g., from an HTML form), use getParsedBody():

Example

<?php
$app->post('/form', function ($request, $response) {
    $data = $request->getParsedBody();
    $name = $data['name'] ?? 'Guest';
    return $response->write("Hello, $name");
});
?>

Handling Response Body

  • Write to Response Body: You can send a response by writing directly to the response object:

Example

<?php
$app->get('/response', function ($request, $response) {
    return $response->write('This is the response body');
});
?>
  • Return JSON Response: To send a JSON response, use withJson():

Example

<?php
$app->get('/json-response', function ($request, $response) {
    $data = ['name' => 'John', 'status' => 'active'];
    return $response->withJson($data);
});
?>
  • Send Custom Response Status and Headers: Modify the response status code and add headers:

Example

<?php
$app->get('/custom-response', function ($request, $response) {
    return $response->withStatus(201)
                    ->withHeader('Content-Type', 'application/json')
                    ->write('{"message": "Created"}');
});

?>

Related Questions & Topics