在 Slim Framework 中通过 PUT 请求更新用户配置文件时出现问题

问题描述 投票:0回答:1

我正在开展一个学校项目,其中使用 PHP、Slim Framework 和 MySQL 来开发 API。我在尝试通过对 /updateuser/{cc_105_id} 端点的 PUT 请求更新用户配置文件时遇到问题。

当我发送 PUT 请求并在 Postman 中填写必填字段(cc_105_profile、cc_105_fullname、cc_105_email)时,我始终收到以下错误消息: { “错误”:正确, "message": "必需参数 cc_105_profile、cc_105_fullname、cc_105_email 缺失或为空" }

这是它的相关代码: `$app->put('/updateuser/{cc_105_id}', function(请求 $request, 响应 $response, 数组 $args){

    $cc_105_id = $args['cc_105_id'];

    // Check if required parameters are present and not empty
    if(!haveEmptyParam($request, $response, array('cc_105_profile', 'cc_105_fullname',         'cc_105_email'))) {

        $request_data = $request->getParsedBody();
        error_log(print_r($request_data, true));

        // Log the values of cc_105_profile, cc_105_fullname, and cc_105_email
        error_log("cc_105_profile: " . $request_data['cc_105_profile']);
        error_log("cc_105_fullname: " . $request_data['cc_105_fullname']);
        error_log("cc_105_email: " . $request_data['cc_105_email']);

        $cc_105_profile = $request_data['cc_105_profile'];
        $cc_105_email = $request_data['cc_105_email'];
        $cc_105_fullname = $request_data['cc_105_fullname'];

        // Fetch the user's username from the database or request data
        // Assuming it's present in the request data, adjust this according to your setup
        $cc_105_username = $request_data['cc_105_username']; 

        // Process the update operation here
        $db = new DbOperations;

        // Check the result of the update operation
        if ($db->updateuser($cc_105_profile, $cc_105_fullname, $cc_105_email, $cc_105_id)) {
        // If the update was successful
        $response_data = array(); 
        $response_data['error'] = false; 
        $response_data['message'] = 'User Updated Successfully';
        $user = $db->getUserByUsername($cc_105_email); // Fetch user by username
        $response_data['user'] = $user; 

        $response->getBody()->write(json_encode($response_data));
        return $response->withHeader('Content-type', 'application/json')->withStatus(200);
    } else {
        // If the update failed
        $response_data = array(); 
        $response_data['error'] = true; 
        $response_data['message'] = 'Please try again later';
        $user = $db->getUserByUsername($cc_105_email); // Fetch user by email
        $response_data['user'] = $user; 
        $response->getBody()->write(json_encode($response_data));
        return $response->withHeader('Content-type', 'application/json')->withStatus(200);
        }
    }  
    return $response
    ->withHeader('Content-type', 'application/json')
    ->withStatus(200);
});

function haveEmptyParam($request, $response, $required_params) {
    $error = false; 
    $error_params = '';
    $request_params = $request->getParsedBody(); 

    foreach($required_params as $param) {
        if(!isset($request_params[$param]) || strlen($request_params[$param]) <= 0) {
            $error = true; 
            $error_params .= $param . ', ';
        }
    }

    if($error) {
        $error_detail = array();
        $error_detail['error'] = true; 
        $error_detail['message'] = 'Required parameters ' . substr($error_params, 0, -2) . '     are missing or empty';
        $response->getBody()->write(json_encode($error_detail));
        return $response->withHeader('Content-type', 'application/json')->withStatus(400);
    }
return $error; 
}

`

我尝试过的:

我确保请求正文中存在必填字段(cc_105_profile、cc_105_fullname、cc_105_email)。 我检查了 hasEmptyParam 函数以确保它正确检查空参数。 预期结果:

我希望代码能够成功更新用户配置文件并返回包含成功消息和更新的用户详细信息的响应。

实际结果:

尽管为所需参数提供了值,但代码始终执行 hasEmptyParam 代码块,表明所需参数丢失或为空。

附加信息:

我附上了我的邮递员请求设置的图像以供参考。 如何解决此问题并确保用户配置文件成功更新?

backend slim
1个回答
0
投票

确保添加 BodyParsing 中间件:

$app->addBodyParsingMiddleware();

https://www.slimframework.com/docs/v4/middleware/body-parsing.html

© www.soinside.com 2019 - 2024. All rights reserved.