【问题标题】:I can send data using body raw but not using formdata using post method in postman我可以使用 body raw 发送数据,但不能使用 postman 中的 post 方法使用 formdata
【发布时间】:2021-09-22 12:44:38
【问题描述】:

我只是想用我的更新个人资料 api 上传个人资料图片。我可以使用 json (post) 方法通过 body raw 简单地发送数据,但是对于上传文件,我使用的是 formdata,因为我无法发送任何数据。我只收到请提供输入详细信息的回复。

这是我的 api 控制器代码

 public function update_profile()
{
    $this->default_file();
    $responseData = array();
    if(!empty($_POST['u_id']))
    {
        $id = $_POST['u_id'];
        $userData['u_id'] = $id;
        $userData['username'] = $_POST['username'];
        $userData['usermob'] = $_POST['usermob'];
        $userData['userlocation'] = $_POST['userlocation'];

        $update_profile = $this->apm->update_profile($userData);

        if(!empty($update_profile))
        {
            $id = $_POST['u_id'];
            $userDetails = array();
            $userDetails['id'] = $id;
            $getUserDetails = $this->apm->getUserDetails($userDetails);

            $responseData['u_id'] = $getUserDetails['result']['u_id'];
            $responseData['username'] = $getUserDetails['result']['username'];
            $responseData['useremail'] = $getUserDetails['result']['useremail'];
            $responseData['usermob'] = $getUserDetails['result']['usermob'];
            $responseData['userlocation'] = $getUserDetails['result']['userlocation'];

            $responseArray = array(
                'apiName' => 'update profile',
                'version' => '1.0.0',
                'responseCode' => 200,
                'responseMessage' => "Your profile updated successfully",
                'responseData' => $responseData
            );
        }
        else
        {
            $responseArray = array(
                'apiName' => 'update profile',
                'version' => '1.0.0',
                'responseCode' => 204,
                'responseMessage' => "error in updating profile",
                'responseData' => null//$responseData
            );
        }
    }
    else
    {
        $responseArray = array(
            'apiName' => 'update profile',
            'version' => '1.0.0',
            'responseCode' => 204,
            'responseMessage' => "Sorry, please provide your input details.",
            'responseData' => null//$responseData
        );
    }
    echo json_encode($responseArray);
    die();
}

我正在提交代码而不添加图像部分代码以使用formdata检查简单的提交数据。

这是我的 api 模式代码

public function update_profile($userData)
{
    return $this->db->update('users', $userData, array('u_id' => $userData['u_id']));
}

public function getUserDetails($userDetails = array())
{
    $arrData = array();
    if($userDetails['id'])
    {
        $where = "u_id='". $userDetails['id']."'";
        $this->db->select('*');
        $this->db->from('users');
        $this->db->where($where);
        $result = $this->db->get()->result_array();
        if(!empty($result))
        {
            $arrData['result'] = $result[0];
        }
        else
        {
            $arrData['result'] = '';
        }
    }
    return $arrData;
}

这是默认文件代码

 function default_file(){
    header("Access-Control-Allow-Origin: * ");
    header("Access-Control-Allow-Headers: Origin,Content-Type ");
    header("Content-Type:application/json ");
    $rest_json = file_get_contents("php://input");
    $_POST = json_decode($rest_json,true);
}

请帮助我使用 formdata 运行我的代码,以便我可以通过该 api 上传图像。

【问题讨论】:

    标签: php api postman codeigniter-3


    【解决方案1】:

    FormData 对象不编码为 JSON(这是明智的,因为 JSON 不支持 File 数据类型,而 FormData 的一大好处是它支持)。

    FormData 对象将被编码为multipart/form-data,它将被 PHP 自动解析并用于填充 $_POST$_FILES

    不幸的是,您的代码 ($_POST = json_decode($rest_json,true);) 用结果覆盖了您想要的数据,以尝试将非 JSON 的内容解析为 JSON。

    注意:使用 FormData 时,请确保不要覆盖请求上的 Content-Type 标头。

    【讨论】:

    • 非常感谢先生
    • 完成先生删除行 $this->default_file() 并且代码工作正常。
    猜你喜欢
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 2014-08-01
    • 2018-01-20
    • 2018-01-13
    • 1970-01-01
    • 2011-10-03
    • 2021-08-14
    相关资源
    最近更新 更多